ListView适配器NullPointer异常

时间:2016-03-26 20:49:52

标签: android listview

我正在尝试实现Navigation抽屉,并且在listview适配器中创建Null-Pointer异常。请告诉我它为什么这样做?

Navigation_drawer.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/listview"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>


  </LinearLayout>

textdata.xml

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="16dip"
     />

           </LinearLayout>

主要活动

在activity_main.xml文件中,我添加了一些其他代码。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};

    DrawerLayout   mdrawer=(DrawerLayout) findViewById(R.id.drawer_layout);

    ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.textdata,mobileArray);
    ListView drawerlist=(ListView) findViewById(R.id.listview);
    drawerlist.setAdapter(adapter);

日志

      01-01 03:46:37.180 19591-19591/com.example.shahik.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shahik.myapplication/com.example.shahik.myapplication.MainActivity}: java.lang.NullPointerException
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                                                                                  at android.app.ActivityThread.access$700(ActivityThread.java:159)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                  at android.os.Looper.loop(Looper.java:176)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: java.lang.NullPointerException
                                                                                  at com.example.shahik.myapplication.MainActivity.onCreate(MainActivity.java:44)
                                                                                  at android.app.Activity.performCreate(Activity.java:5372)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
                                                                                  at android.app.ActivityThread.access$700(ActivityThread.java:159) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                  at android.os.Looper.loop(Looper.java:176) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5419) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 

2 个答案:

答案 0 :(得分:0)

你能提供logcat输出吗?
也尝试用它的父母&#34;来实例化抽屉。查看即

ListView drawerlist=(ListView) mdrawer.findViewById(R.id.listview);

答案 1 :(得分:0)

这就是你的布局名称 - &gt; Navigation_drawer.xml

那就是你设置的内容视图 - &gt; activity_main.xml中

我猜你错过了主要布局。