如何在列表视图的标题布局中使用TextView?

如何在列表视图的标题布局中使用TextView?

问题描述:

我定制的ListView头视图通过使用XML文件,该文件是:如何在列表视图的标题布局中使用TextView?

<?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资源。 现在的问题是: 为什么我无法获得TextView资源?

postTitle = (TextView)headerLayout.findViewById(R.id.post_title_tv); 
+0

就是这样,它工作,谢谢你,萨米尔。 – Mejonzhan 2012-03-04 09:19:18