我的应用程序中的自定义ArrayAdapter中出现了Nullpointerexception.我不知道为什么在这里提出Nullpointer.我无法重现这个例外.异常没有出现很长时间,我没有代码更改,所以这使它更奇怪.代码是:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在第一行的getView方法中引发了异常.我调试了我的应用程序并使用null参数调用了super.getView,但没有引发Nullpointerexception,所以它看起来super已经为null,但是怎么会这样?StackTrace是:
在显示java.lang.NullPointerException android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)在android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)在de.hartig.abt.activity.BluetoothDeviceList $ MyAdapter.getView( BluetoothDeviceList.java:140)在android.widget.AbsListView.obtainView(AbsListView.java:1408)在android.widget.ListView.makeAndAddView(ListView.java:1727)在android.widget.ListView.fillDown(ListView.java:652 )在Android.widget.List.Loutout(AbsListView.java:1240)的android.widget.List.Loutout(ListView.java:1580)的android.widget.ListView.fillFromTop(ListView.java:709) .view.layout(View.java:7057)位于android.widget.Lineout.ayChildFrame的android.widget.FrameLayout.onLayout(FrameLayout.java:333)android.view.View.layout(View.java:7057) LinearLayout.java:1249)在android.view.View.View的android.widget.LinearLayout.onLayout(LinearLayout.java:1042)的android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125).layout(View.java:7057)位于android.view.View.Retout(MapLayout.java:333)的android.view.View.layout(View.java:7057),位于android.view.ViewRoot.performTraversals(ViewRoot. java:1045)在android.view.Handler.dispatchMessage(Handler.java:99)的android.view.ViewRoot.handleMessage(ViewRoot.java:1727)android.os.Looper.loop(Looper.java:123)at at android.app.ActivityThread.main(ActivityThread.java:4680)位于com.android的java.lang.reflect.Method.invoke(Method.java:521)的java.lang.reflect.Method.invokeNative(Native Method)中. internal.os.ZygoteInit …