6

我想實現一個導航抽屜,但由於某些原因我得到這個空指針異常如何糾正v4.DrawerLayout中的NullPointerException?

我花了很多時間,但徒勞無功。這是我的代碼的一部分。我不明白爲什麼它返回空指針異常。

我需要導入任何庫或其他嗎?

在此先感謝。

package com.motobeans.productions.aloha; 

import android.app.FragmentManager; 
import android.os.Bundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import java.util.Locale; 
import android.app.Fragment; 

import android.support.v7.widget.Toolbar; 
import android.widget.ImageView; 
import android.widget.ListView; 

public class Activity_test extends ActionBarActivity { 
private String[] mPlanetTitles; 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private CharSequence mTitle; 
    private CharSequence mDrawerTitle; 

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

     setContentView(R.layout.activity_test); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 

     mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     mTitle = mDrawerTitle = getTitle(); 

     // Set the adapter for the list view 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mPlanetTitles)); 
     // Set the list's click listener 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 


     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, 
       R.string.drawer_open, R.string.drawer_close) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

    } 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // If the nav drawer is open, hide action items related to the content view 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); 
     return super.onPrepareOptionsMenu(menu); 
    } 

這是logcat的輸出

Process: com.motobeans.productions.aloha, PID: 1330 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.motobeans.productions.aloha/com.motobeans.productions.aloha.Activity_test}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference 
      at com.motobeans.productions.aloha.Activity_test.onCreate(Activity_test.java:74) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5254) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

回答

14

好像調用.setDrawerListener()時mDrawerLayout爲空。

確保您在findViewById(R.id.drawer_layout)和佈局文件中的導航抽屜上使用了相同的ID。

+1

這工作,是我不好,非常感謝! – varunkr

1

類似的問題在https://code.google.com/p/android/issues/detail?id=202478

DrawerLayout.removeDrawerListener()被調用,而不一套 DrawerLayout.DrawerListener

但是Android支持庫,修訂23.2.1的releas後(三月報道,谷歌2016)此問題已解決。

更新支持庫到Android Support Library to 23.2.1或更高版本。

0

使用getSupportActionBar()代替getActionBar()