开发者社区> 问答> 正文

LayoutInflater.inflate 获取 RelativeLayout?报错

今天在用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
还是没明白为什么..........



展开
收起
爱吃鱼的程序员 2020-06-22 17:26:07 501 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你贴的代码和你描述的不一致啊。。。<divclass="ref">

    引用来自“xesam”的答案

    <divclass=ref_body>你贴的代码和你描述的不一致啊。。。你错在哪里啦我现在也遇到了和你相同的问题求指导啊~

    2020-06-22 17:26:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载