Nullpointerexception in getView of custom ArrayAdapter
I'm getting a Nullpointerexception in my custom ArrayAdapter in my App. I do not know why a Nullpointer is raised here. And i can not reproduce this exception. The exception did not appear a long time and i have no code changes, so this makes it even more strange. The code is:
private class MyAdapter<T> extends ArrayAdapter<String> {
private Map<String, String> selectedDevices;
public MyAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
BluetoothDbAdapter dbHelper = new BluetoothDbAdapter(context);
dbHelper.open();
selectedDevices = dbHelper.fetchAllDevicesAsMap();
dbHelper.close();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckedTextView item = (CheckedTextView) view;
CharSequence text = item.getText();
if (selectedDevices.values().contains(text)) {
item.setChecked(true);
}
return item;
}
}
The Exception is raised in getView method in the first line. I have debugged my App and called super.getView with null parameters but no Nullpointerexception was raised, so it seemes that super have been null, but how can this be??? The StackTrace is:
java.lang.NullPointerException at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) at de.hartig.abt.activity.BluetoothDeviceList$MyAdapter.getView(BluetoothDeviceList.java :140) at android.widget.AbsListView.obtainView(AbsListView.java:1408) at android.widget.ListView.makeAndAddView(ListView.java:1727) at android.widget.ListView.fillDown(ListView.java:652) at android.widget.ListView.fillFromTop(ListView.java:709) at android.widget.ListView.layoutChildren(ListView.java:1580) at android.widget.AbsListView.onLayout(AbsListView.java:1240) at android.view.View.layout(View.java:7057) at android.widget.FrameLayout.onLayout(FrameLayout.java:333) at android.view.View.layout(View.java:7057) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) at android.view.View .layout(View.java:7057) at android.widget.FrameLayout.onLayout(FrameLayout.java:333) at android.view.View.layout(View.java:7057) at android.view.ViewRoot.performTraversals(ViewRoot.java:1045) at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4680) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)
Does any body know how i can solve this problem? Thanks for help!!!
You are probably calling add
with a null
value and the ArrayAdapter
is choking on it. To diagnose the problem further we'd need to see the code for fetchAllDevicesAsMap
or wherever you are adding elements to the ArrayAdapter
.
An excerpt from ArrayAdapter.java inside createViewFromResource
:
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
This indicates that ArrayAdapter
will succumb to a NullPointerException
if one of the elements in the underlying array is null
.
From the stack trace you can clearly see that super
was not null, as it did go into android.widget.ArrayAdapter.getView()
.
You can look at the source for android.widget.ArrayAdapter.createViewFromResource
line 355 to try to determine the true cause of your null pointer.
UPDATE
I took a peek at the code and it is failing on this line text.setText(item.toString());
so either text
or item
are null.
You can look at the code yourself here: android.widget.ArrayAdapter source
链接地址: http://www.djcxy.com/p/16630.html上一篇: 显示完整的logcat