2016-03-26 20 views
0

Ich versuche, Navigationsschublade zu implementieren, und im Listenansichtsadapter erstellt Null-Zeiger-Ausnahme. Sag mir bitte, warum es so ist?ListView Adapter NullPointer Ausnahme

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> 

Hauptaktivität

Bei activity_main.xml Datei Ich habe einige andere Code hinzugefügt.

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); 

Log

 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)  
+0

bitte zeigen Sie Ihr Log –

Antwort

0

Können Sie die logcat Ausgang?
Händler und instanziiert die drawerlit mit seinen "Eltern" Ansicht dh

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

Danke, ich habe dies versucht, aber immer noch die gleiche Ausnahme bei SetAdapter-Funktion. – Shahek

0

Das ist der Name des Layouts -> Navigation_drawer.xml

Und das ist die InhaltAlle Sie setzen -> activity_main.xml

Ich denke, du bist das Hauptlayout fehlt.

+0

Nein, ich habe anderen Code in main_layout und sogar ändern Aktivität Haupt mit Navigation Schublade noch App stürzt ab. – Shahek