简体   繁体   中英

NPE at getSupportActionBar() on OnePlus One Running Android 5.1

My user who's using OnePlus One (A0001) running Android 5.1 reported this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.ActionBar android.support.v7.app.ActionBarActivity.getSupportActionBar()' on a null object reference
    at com.imincode.meniti.MenuHome.onCreateOptionsMenu(MenuHome.java:558)
    at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2044)

Line 558 in my code is:

ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();

Apparently he's the only one with this problem. Other devices never encountered such error. How do I fix this?

This solution suggests we use

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

but then how would the actionBar behaves on devices that returns null? How do we set setDisplayShowTitleEnabled and setDisplayHomeAsUpEnabled on such devices?

In my OnCreateView, I have this line:

toolbar = (Toolbar) (getActivity()).findViewById(R.id.tool_bar); 
if (toolbar != null) {
   ((ActionBarActivity)getActivity()).setSupportActionBar(toolbar);
}

Btw here's the full stack trace:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.ActionBar android.support.v7.app.ActionBarActivity.getSupportActionBar()' on a null object reference
    at com.imincode.meniti.MenuHome.onCreateOptionsMenu(MenuHome.java:558)
    at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2044)
    at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:2063)
    at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:270)
    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:263)
    at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
    at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
    at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
    at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
    at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5278)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:375)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

It is likely that you are using a theme that has no ActionBar, while you are not using ToolBar as well.

If you want to use ActionBar, check your theme

If you are using ToolBar, you have to call setSupportActionBar(toolbar) before calling getSupportActionBar()

Edit:

According to your edit, your Toolbar is inside layout of Fragment ,

To solve your problem, use one of these methods:

  1. Move your toolbar to layout of Activity and call setSupportActionBar in onCreate of Activity ; Or
  2. Do not use setSupportActionBar or getSupportActionBar or anything related to ActionBar , use Toolbar as Toolbar, eg call toolbar.inflateMenu to inflate menu

Edit 2

Looking at your stack trace,

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.ActionBar android.support.v7.app.ActionBarActivity.getSupportActionBar()' on a null object reference

It is caused by getActivity() returning null.

Seems you are trying to call getActivity() in Fragment#onCreateOptionMenu , you should not try to access Activity in the method.

Move those methods really need Activity to onActivityCreated instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM