2015-09-07 65 views
3

我採取抽屜式導航欄在安卓:NullPointerException異常在getActionBar

getActionBar().setDisplayShowHomeEnabled(true); 
getActionBar().setHomeButtonEnabled(true); 

滑動菜單.getting java.lang.NullPointerException在我的應用我都試過活動擴展ActivityAppCompatActivity

這裏是我的onCreate代碼

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     mTitle = mDrawerTitle = getTitle(); 

     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 
     navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 
     navDrawerItems = new ArrayList<NavDrawerItem>(); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22")); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+")); 
     navMenuIcons.recycle(); 
     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 
     adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); 
     mDrawerList.setAdapter(adapter); 
     //abling action bar app icon and behaving it as toggle button 
     getActionBar().setDisplayShowHomeEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 

       R.drawable.menu_left, //nav menu toggle icon 
       R.string.app_name, // nav drawer open - description for accessibility 
       R.string.app_name // nav drawer close - description for accessibility 
     ) 
     { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       // calling onPrepareOptionsMenu() to show action bar icons 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       // calling onPrepareOptionsMenu() to hide action bar icons 
       invalidateOptionsMenu();} 
     }; 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 
     if (savedInstanceState == null) { 
      // on first time display view for first nav item 
      displayView(0); 
     } 
    } 

這裏是logcat的inforamtion

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tazeen.slidingmenu/com.example.tazeen.slidingmenu.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
      at android.app.ActivityThread.access$600(ActivityThread.java:130) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4745) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.example.tazeen.slidingmenu.MainActivity.onCreate(MainActivity.java:62) 
      at android.app.Activity.performCreate(Activity.java:5008) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
            at android.app.ActivityThread.access$600(ActivityThread.java:130) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4745) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

嘗試發佈您的Logcat輸出也。 –

+0

[getActionBar())可能的重複返回null](http://stackoverflow.com/questions/6867076/getactionbar-returns-null) – alfasin

回答

2

如果從AppCompatActivity你應該使用延伸getSupportActionBar()

如果要擴展形式Activity,你可以用getActionBar(),

但是在這兩種情況下,

您必須確保爲您的Activity分配了支持ActionBar的主題。

如:Theme.AppCompat.Light

,或者您可以通過編程做,

requestWindowFeature(Window.FEATURE_ACTION_BAR); 

super.onCreate(savedInstanceState); 

但最好是有一個風格的聲明,並分配給您的活動,而不是做它以編程方式。

+0

@androidTag你解決了這個問題嗎? –

0

extends簡單Activity代替ActionBarActivity。而請setDisplayShowHomeEnabled()像以前那樣經常檢查空:

if(getActionBar()!=null){ 
    getActionBar().setDisplayShowHomeEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
    } 

,另一個解決方案是使用getSupportActionBar()代替getActionBar() 然後

if(getSupportActionBar()!=null){ 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getgetSupportActionBarActionBar().setHomeButtonEnabled(true); 
    } 

享受你的代碼:)

相關問題