我使用xml文件在ListView
中自定义了标题视图,此文件为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/post_title_tv"
android:paddingLeft="8.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post title"
android:layout_alignParentLeft="true" />
<ImageView
android:id="@id/post_fave_image"
android:paddingLeft="10.0dip"
android:paddingRight="8.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/btn_star_big_on"
android:layout_alignParentRight="true" />
</RelativeLayout>
然后,我将此代码添加到Listview
中:
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
RelativeLayout headerLayout = (RelativeLayout)layoutInflater.inflate( R.layout.post_titl, null, false );
postTitle = (TextView) findViewById(R.id.post_title_tv);
postTitle.setText(postName);
然后,当我运行它时,我收到此错误:
03-04 14:52:41.946: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.client.ui/com.google.client.ui.PostListView}: java.lang.NullPointerException
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.os.Looper.loop(Looper.java:123)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-04 14:52:41.946: E/AndroidRuntime(484): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 14:52:41.946: E/AndroidRuntime(484): at java.lang.reflect.Method.invoke(Method.java:507)
03-04 14:52:41.946: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-04 14:52:41.946: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 14:52:41.946: E/AndroidRuntime(484): at dalvik.system.NativeStart.main(Native Method)
03-04 14:52:41.946: E/AndroidRuntime(484): Caused by: java.lang.NullPointerException
03-04 14:52:41.946: E/AndroidRuntime(484): at com.google.client.ui.PostListView.setComponent(PostListView.java:105)
03-04 14:52:41.946: E/AndroidRuntime(484): at com.google.client.ui.PostListView.onCreate(PostListView.java:53)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 14:52:41.946: E/AndroidRuntime(484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这是NullPointerException
,我无法获取TextView
中的post_title.xml
资源。 问题是:
为什么我不能获得TextView
资源?
答案 0 :(得分:1)
postTitle = (TextView)headerLayout.findViewById(R.id.post_title_tv);