本文介绍了机器人 - ArrayAdapter.createViewFromResource(INT,查看,ViewGroup中,INT)线:355 NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置一个ArrayAdapter,我得到了以下错误:

I'm setting an ArrayAdapter and I get the following error:

 Thread [<1> main] (Suspended (exception NullPointerException))
ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 355
ArrayAdapter.getView(int, View, ViewGroup) line: 323
ListView(AbsListView).obtainView(int, boolean[]) line: 1294
ListView.measureHeightOfChildren(int, int, int, int, int) line: 1198

我的symptomremedyActivity.java是这样的:

My symptomremedyActivity.java looks like:

    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布局是这样的:

and my symptomremedy.xml layout looks like:

<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布局是这样的:

and menu_item.xml layout looks like:

<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"
 />

我已经做了其他的活动,没有任何问题类似的东西,所以我不明白,为什么有一个问题在这里。所有涉及的领域都是非空,即remedyArray等人有任何想法是什么问题?

I've done something similar in other Activities with no problems, so I can't figure out why there is a problem here. All of the fields involved are non-null, i.e. remedyArray, etc. Anyone have any idea what the problem is?

推荐答案

原来的问题是在我创建了数组一个ArrayAdapter的方式。 previously的code我是:

Turns out the problem was in the way I was creating the array for the ArrayAdapter. Previously the code I had was:

String remedyArray[] = new String[30];

当我改变了code到:

When I changed the code to:

    String remedyArray[] = new String[30];
    for ( int i = 0; i< 30; i++){
            remedyArray[i] = "";
    }

一切正常。我不完全理解为什么,但它似乎与初始化值的数组做的伎俩。我猜的Java的独特性之一是创建数组时,该条目包含空,我认为​​这是导致了该问题的空值。

everything worked. I don't completely understand why, but it appeared that initializing the array with values did the trick. I guess one of the uniquenesses of Java is that when an array is created, the entries contain null, and I think it was those null values that were causing the problem.

这篇关于机器人 - ArrayAdapter.createViewFromResource(INT,查看,ViewGroup中,INT)线:355 NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:14