android - 具有自定义项的 ListView

标签 android android-layout android-listview

我仔细研究了这个问题,因为你们是如此自信,我发现我在输入问题时输入了错误的布局名称(对此感到抱歉)

我拥有的正确内容 View 是这样的:

setContentView(R.layout.main);

结束编辑

我在 R.Java 中看到了我的 ID:

public static final int txtReadMore_link=0x7f06000e;

更新:

03-19 17:05:07.158: E/AndroidRuntime(1356): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.website.playlist_activity}: java.lang.NullPointerException
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.os.Looper.loop(Looper.java:123)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invoke(Method.java:507)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at dalvik.system.NativeStart.main(Native Method)
03-19 17:05:07.158: E/AndroidRuntime(1356): Caused by: java.lang.NullPointerException
03-19 17:05:07.158: E/AndroidRuntime(1356):     at net.website.playlist_activity.onCreate(playlist_activity.java:101)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 17:05:07.158: E/AndroidRuntime(1356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

我正在尝试从自定义 ListView 访问 textview,下面是代码,我收到空引用错误...我如何访问 txtReadMore_link

当用户点击链接时显示新 Activity 。

播放列表 Activity

private TextView link;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_row);

       //throwing error - NULL????
       link = (TextView)findViewById(R.id.txtReadMore_link);

       link.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        int duration = Toast.LENGTH_SHORT;
        TextView tx =(TextView)v.findViewById(R.id.txtReadMore_link); 
        Toast toast = Toast.makeText(playlist_activity.this, tx.getText(), duration);
        toast.show();               
        }
    });
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"></ListView>    
</LinearLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"      
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg" 
        android:layout_marginRight="5dip">

        <ImageView     
            android:id="@+id/list_image"   
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/rihanna"/>

    </LinearLayout>

    <!-- Title Of Song-->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Rihanna Love the way lie"
        android:textColor="#040404"
        android:typeface="sans" 
        android:textSize="15dip"
        android:textStyle="bold"/>

    <!-- Artist Name -->
    <TextView
        android:id="@+id/artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:textColor="#343434"
        android:textSize="10dip"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Just gona stand there and ..." />


       <TextView
            android:id="@+id/txtReadMore_link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_margin="5dp" 
            android:clickable="true"
            android:text="Click here to read more.."
            android:textSize="9sp" />



</RelativeLayout>

最佳答案

只需清理您的项目并像这样重新运行它:在您的 Eclipse IDE 中,单击 项目 ==> 清理 ==> 检查你的项目 ==> 按确定 。并再次运行您的项目,它会起作用;)

编辑:

你有NullPointerException因为在你的方法中 setContentView() ,您已将 Activity 布局指定为:main.xml

而你正试图找到一个 View通过不在您的布局中的 id main.xml (它在你的 list_row.xml 上),确认你已经指定了你的 Activity 的确切布局。 ,或尝试声明您的 TextView在你的布局中main.xml

关于android - 具有自定义项的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775173/

相关文章:

android - 自定义适配器和 ListView 崩溃

android - R.java 中的值在运行期间或运行之间是否会发生变化?

Android - 在哪里放置应用程序的一般信息/印记/联系信息/关于?

java - 静态变量作为全局变量,静态对象何时被垃圾收集?

Android - 如何为所有页面添加标题?

android - 有没有办法为 Mapfragment 实现圆角?

android - 如何使用 AppCompat 样式隐藏 Listview 的滚动条

android - 使用自定义 ListView 打开对话框

android createTempFile 抛出权限被拒绝?

android - 避免列表重复