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构造函数.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...