微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – ArrayAdapter.createViewFromResource(int,View,ViewGroup,int)行:355 NullPointerException

我设置了一个ArrayAdapter,并且我收到以下错误
Thread [<1> main] (Suspended (exception NullPointerException)) 
ArrayAdapter.createViewFromresource(int,View,ViewGroup,int) line: 355    
ArrayAdapter.getView(int,ViewGroup) line: 323    
ListView(AbsListView).obtainView(int,boolean[]) line: 1294 
ListView.measureHeightOfChildren(int,int,int) line: 1198

我的symptomremedyActivity.java看起来像:

setContentView(R.layout.symptomremedy);
    ListView remedyList = (ListView) findViewById(R.id.ListView_Symptomremedy);
    remedyList.setTextFilterEnabled(true);
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item,remedyArray);
    remedyList.setAdapter(adapt);

我的symptomremedy.xml布局如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Button_BACK"
android:layout_width="wrap_content"
android:layout_height="@dimen/menu_button_height"
android:layout_alignTop="@+id/Button_BACK"
android:layout_below="@+id/ImageView_MenuHeader"
android:textSize="@dimen/menu_button_text_size"
android:text="@string/back"></Button>

<ListView
        android:layout_height="wrap_content"
        android:id="@+id/ListView_Symptomremedy"
        android:layout_width="fill_parent"
        android:background="@color/menu_white"
        android:divider="@drawable/straight_divider"
        android:listSelector="@drawable/textured"
        android:layout_alignParentTop="true"></ListView>

</LinearLayout>

和menu_item.xml布局看起来像:

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:textSize="@dimen/menu_item_size"
android:text="test string"
android:textColor="@color/menu_black"
android:layout_height="fill_parent"
 />

我在其他活动中做过类似的事情没有任何问题,所以我不知道为什么在这里一个问题.所有涉及的字段都是非空的,即补救数组等.任何人都知道问题是什么?

解决方法

NullPointerException in
ArrayAdapter.createViewFromresource

表明问题来自于

R.layout.menu_item

如果此文件存在于/ res / layout /下,请检查它是否已在R.java中注册

另外,您是否使用自定义的ArrayAdapter?如果是这样,请使用代码更新问题.

如果您不使用自定义的ArrayAdapter,请尝试在menu_item中的TextView中添加标识符.如果您不覆盖getView,Android将尝试使用remedyList中的数据填充resourceId(menu_item.xml)中的TextView.我从来没有这样做,但您可能需要使用此构造函数指定TextView id:

ArrayAdapter(Context context,int resource,int textViewResourceId,List<T> objects)

检查ArrayAdapter构造函数.

原文地址:https://www.jb51.cc/android/311513.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐