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
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);
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>
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"
/>
I've done something similar in other 开发者_如何学GoActivities 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?
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];
When I changed the code to:
String remedyArray[] = new String[30];
for ( int i = 0; i< 30; i++){
remedyArray[i] = "";
}
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.
The NullPointerException in
ArrayAdapter.createViewFromResource
suggests that the problem is coming from
R.layout.menu_item
If this file exists under /res/layout/, check that it's registered in R.java
Also, are you using a custom ArrayAdapter? If so, please update the question with the code.
If you are not using a custom ArrayAdapter, try adding an identifier to the TextView in menu_item. If you are not overriding getView, Android will try to fill a TextView in the resourceId (menu_item.xml) with the data in remedyList. I've never done this, but you may need to specify the TextView id by using this constructor:
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Check ArrayAdapter constructors.
My problem was that the custom item layout and the dropdown item layout weren't using the same Id for the main textview; basically because I wanted to create a custom item, but maintain the default dropdown item.
the id in textViewResourceId must be the same for both views.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Creating a custom dropdown item with the same textview id solved the problem.
Sorry for digging up this question but I came across the issue today and I understood why this happens.
If you statically allocate some space for a newly created array (ie. String[] array = new String[80]
) you have to make sure that you fill all the 81 places of the array. Because if you do not they will be filled with null
values thus causing the NullPointerException
.
When you filled the array with empty strings you basically found a work-around over this.
精彩评论