自定义ArrayAdapter的getView中的Nullpointerexception

use*_*571 9 android nullpointerexception android-arrayadapter

我的应用程序中的自定义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 $ MethodAndArgsCaller.run(ZygoteInit.java:858)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)在dalvik.system.NativeStart.main(本机方法)

有没有人知道我怎么能解决这个问题?感谢帮助!!!

Mat*_*lis 14

你可能正在调用add一个null值,并且它ArrayAdapter正在窒息.为了进一步诊断问题,我们需要查看代码,fetchAllDevicesAsMap或者在哪里添加元素ArrayAdapter.

从摘录ArrayAdapter.javacreateViewFromResource:

T item = getItem(position);
if (item instanceof CharSequence) {
    text.setText((CharSequence)item);
} else {
    text.setText(item.toString());
}
Run Code Online (Sandbox Code Playgroud)

这表明ArrayAdapter将屈服于NullPointerException底层数组中的一个元素是null.


Jus*_*ugh 2

从堆栈跟踪中您可以清楚地看到它super不为空,因为它确实进入了android.widget.ArrayAdapter.getView().

您可以查看android.widget.ArrayAdapter.createViewFromResource第 355 行的源代码,尝试确定空指针的真正原因。

更新

我看了一下代码,它在这一行失败了,text.setText(item.toString());所以要么text要么item为空。

您可以在这里自己查看代码: android.widget.ArrayAdapter 源