ActionBarActivity getSupportActionBar().hide() throws NullPointerException

泄露秘密 提交于 2019-12-07 10:05:11

问题


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


回答1:


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. : )




回答2:


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>



回答3:


In short: <item name="android:windowNoTitle">true</item> may be used with Activity only, NOT ActionBarActivity

On the other hand,getSupportActionBar().hide(); may be used with ActionBarActivity




回答4:


Having Theme.AppCompat.Light both for pre-ICS and post-ICS will definitely cause issues. The way of doing this is to set a MyTheme in:

  1. values/styles.xml. This will extend from Theme.AppCompat.Light. Ex: <style name="MyTheme" parent="Theme.AppCompat.Light">
  2. values-v11/styles.xml. This will extend from a Holo theme. Ex: <style name="MyTheme" parent="android:Theme.Holo.Light">

In this way, Android knows what kind of theme to load based on runtime device host API.

Don't call getActionBar() once you extend from ActionBarActivity. On post-ICS devices this will run, but for lower API devices you will get a runtime not supported method Exception or something equivalent.



来源:https://stackoverflow.com/questions/20147921/actionbaractivity-getsupportactionbar-hide-throws-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!