今天在用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还是没明白为什么..........
你贴的代码和你描述的不一致啊。。。<divclass="ref">
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。