LayoutInflater.inflate 获取 RelativeLayout 后,获取其中的TextView 时出现一个问题

Java_Liam 发布于 2013/08/19 19:02
阅读 3K+
收藏 0

今天在用LayoutInflater.inflate() 获取RelatioveLayout后,再获取其中的TextView时,能用getID()获取起ID, 但是setText() 时,出现NullPointException.......

下面是 界面的xml文件:

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="@dimen/img_width"
        android:layout_height="@dimen/img_height"
        android:layout_marginRight="@dimen/img_margin_right" />

    <TextView
        android:id="@+id/nameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/img"
        android:layout_toRightOf="@id/img" />

    <TextView
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nameText"
        android:layout_toRightOf="@id/nameText" />

    <RatingBar
        android:id="@+id/score"
        style="?android:attr/ratingBarStyleSmall"
        android:isIndicator="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/img"
        android:layout_alignBottom="@id/img" />

</RelativeLayout>
实现LayoutInflater:

this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
获取RelativeLayout:

convertView = this.inflater.inflate(R.layout.list_item, null);
获取view中的ImageView: 

imgView = (ImageView) convertView.findViewById(R.id.img);
Log.i("Tester", String.valueOf(imgView.getId()));
输出ImageView的id

08-19 18:26:40.755: I/Tester(1424): name: 2131230722
与ImageView的id一致:       

public static final int img=0x7f080001;

但是后来用 setText() 方法时

imgView.setImageResource(R.drawable.loadingbar);
结果,报错了:

08-19 18:26:40.885: E/AndroidRuntime(1424): java.lang.NullPointerException
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at com.Liam.servicer.MyListAdapter.getView(MyListAdapter.java:63)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.AbsListView.obtainView(AbsListView.java:2012)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.ListView.onMeasure(ListView.java:1155)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.View.measure(View.java:12603)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.View.measure(View.java:12603)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.View.measure(View.java:12603)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.View.measure(View.java:12603)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.View.measure(View.java:12603)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.os.Looper.loop(Looper.java:137)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at android.app.ActivityThread.main(ActivityThread.java:4340)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at java.lang.reflect.Method.invokeNative(Native Method)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at java.lang.reflect.Method.invoke(Method.java:511)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-19 18:26:40.885: E/AndroidRuntime(1424): 	at dalvik.system.NativeStart.main(Native Method)
08-19 18:26:44.825: I/Process(1424): Sending signal. PID: 1424 SIG: 9
还是没明白为什么..........



以下是问题补充:

@Java_Liam:忘了说了,我最后在setText()方法前测试了一下 if (imgView == null) { Log.i("Tester", "Shut up!!!!!!!!!!!"); } 结果打印出:08-19 18:26:40.855: I/Tester(1424): Shut up!!!!!!!!!!! (2013/08/19 19:06)
加载中
0
xesam
xesam
你贴的代码和你描述的不一致啊。。。
0
Java_Liam
Java_Liam

引用来自“xesam”的答案

你贴的代码和你描述的不一致啊。。。
呃,准确的说,有ImageView,也有TextView,现在已经明白什么地方错了
G
Goirry
你错在哪里啦 我现在也遇到了和你相同的问题 求指导啊~
返回顶部
顶部