0
我定製的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
資源?
就是這樣,它工作,謝謝你,薩米爾。 – Mejonzhan 2012-03-04 09:19:18