Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarActivity getSupportActionBar().hide() throws NullPointerException

Call

 if (getSupportActionBar() != null)
     getSupportActionBar().hide();

or just:

getActionBar()

in android.support.v7.app.ActionBarActivity I get such exception:

    ...
    java.lang.NullPointerException
    at android.support.v7.app.ActionBarImplICS.hide(ActionBarImplICS.java:302)
    at android.support.v7.app.ActionBarImplJB.hide(ActionBarImplJB.java:20)
    ...

EDIT: it just happens when activity have Theme:

<style name="MyTheme" parent="Theme.AppCompat.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
    </style>

note:

getSupportActionBar()

do not return null

like image 412
Abi- Avatar asked Nov 22 '13 15:11

Abi-


People also ask

How to remove top Action bar in android?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.

How to hide toolbar in activity?

So basically we just need to change DarkActionBar to NoActionBar. NoActionBar theme prevents the app from using the native ActionBar class to provide the app bar. Thus it removes the title of every activity.

How to hide Action bar in android Kotlin?

ActionBar actionBar = getActionBar(); actionBar. hide();


2 Answers

meet the same problem ,but I use code to set fullscreen and noActionbar below instead of theme in xml:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    setContentView(R.layout.page_welcome);
    initViews();
}

this code runs well before ICS but crashs caused by NullPointException above ICS,After some experiments,I got the solution:delete one line code which set no title as below:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    setContentView(R.layout.page_welcome);
    initViews();
}

Then it works well at all platforms. : )

like image 103
ruidge Avatar answered Sep 24 '22 00:09

ruidge


As I understand

if (getSupportActionBar() != null)
     getSupportActionBar().hide();

is no correct! Becouse getSupportActionBar() return not-null instance of (android.support.v7.app.ActionBarImplICS)

after that we can call hide function (getSupportActionBar().hide();)

but inside this function we will have NullPointerException because variable mActionBar inside android.support.v7.app.ActionBarImplICS instance == null http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/support/v7/app/ActionBarImplICS.java#302

As I understand inside constructor of android.support.v7.app.ActionBarImplICS

 mActionBar = activity.getActionBar();

return null, because our Activity does not have ActionBar via Theme

<style name="MyTheme" parent="Theme.AppCompat.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
    </style>
like image 26
Abi- Avatar answered Sep 26 '22 00:09

Abi-