NPE导航抽屉listView抛出空指针异常

时间:2017-03-13 12:59:15

标签: android nullpointerexception navigation-drawer

编辑:我不明白为什么应该像建议的那样复制this一般链接。我解决了几十个NPE,并阅读了几个理论。在SO上非常重要的是,除了我提到的那个之外,还有任何类似的Stacktrace案例。其他帖子引用Spinner而不是Navigation-Drawer。此外,Stacktrace不引用我的代码行,并且String I初始化与数组的长度一致。由于这些原因很难调试给我,我花了整整两天时间。我的问题格式正确,显示研究,因为我提到另一篇SO文章。感谢任何帮助。

编辑2:添加完整的堆栈跟踪作为查询

编辑3:现在它有效! ArrayAdapter正在等待一个List对象的第三个参数,而我提供了一个String [],它是一个不从String继承的Array。 我正在寻找一个导航抽屉,默认情况下加载到ListView字符串List1,然后用户可以添加更多列表。 不幸的是我有这个错误,没有指出我的代码行。 检查Stacktrace和唯一的响应vaguely similar 我得出结论错误应该在这里,没有行

  

String [] provaListaDrawer    但我不确定。

   String[] projection = {" DISTINCT " + UcanContract.Tasks.COLUMN_LIST};
    Cursor cursor=getContentResolver().query(UcanContentProvider.CONTENT_URI,projection,null,null,null);
    String cursorData = DatabaseUtils.dumpCursorToString(cursor);

    ArrayList<String> arrayList = new ArrayList<>();

    while(cursor.moveToNext()) {


        String list = cursor.getString(cursor.getColumnIndexOrThrow(UcanContract.Tasks.COLUMN_LIST));

        arrayList.add(list);


    }
    cursor.close();




    String[] temp={"List1"};

    String[] provaListaDrawer = arrayList.toArray(temp);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_view,  provaListaDrawer));

}
  

E / AndroidRuntime:致命异常:主要                                                                         流程:ivano.android.com.ucan,PID:8856                                                                         java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.String java.lang.Object.toString()&#39;在null对象引用上                                                                             在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)                                                                             在android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)                                                                             在android.widget.AbsListView.obtainView(AbsListView.java:2363)                                                                             在android.widget.ListView.makeAndAddView(ListView.java:1970)                                                                             在android.widget.ListView.fillDown(ListView.java:704)                                                                             在android.widget.ListView.fillFromTop(ListView.java:765)                                                                             在android.widget.ListView.layoutChildren(ListView.java:1744)                                                                             在android.widget.AbsListView.onLayout(AbsListView.java:2162)                                                                             在android.view.View.layout(View.java:17637)                                                                             在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                             在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)                                                                             在android.view.View.layout(View.java:17637)                                                                             在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                             在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                             在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                             在android.view.View.layout(View.java:17637)                                                                             在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                             在com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)                                                                             在android.view.View.layout(View.java:17637)                                                                             在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                             在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                             在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                             在com.android.internal.policy.DecorView.onLayout(DecorView.java:726)                                                                             在android.view.View.layout(View.java:17637)                                                                             在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                             在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)                                                                             在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)                                                                             在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)                                                                             在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6337)                                                                             在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)                                                                             在android.view.Choreographer.doCallbacks(Choreographer.java:686)                                                                             在android.view.Choreographer.doFrame(Choreographer.java:621)                                                                             在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)                                                                             在android.os.Handler.handleCallback(Handler.java:751)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:154)                                                                             在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

现在它工作了ArrayAdapter正在等待一个List对象的第三个参数,而我提供了一个String [],它是一个不从String继承的Array。正如Mike M.建议provaListaDrawer为空,但主要的一点是我无法将String[]传递给ArrayAdapter,我应该传递List<T> object并按照建议ArrayList是完美的,因为继承自List

基本上写作:

 String[] temp={"List1"};

String[] provaListaDrawer = arrayList.toArray(temp);

mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_view,  provaListaDrawer));

我不得不写:

  ArrayList<String> provaListaDrawer = new ArrayList<>();
    provaListaDrawer.add("List1");

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_view,  provaListaDrawer));

当运行时编译执行android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)找到有效参数时,可以避免NPE!