Java ActionBarActivity getSupportActionBar().hide() 抛出 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20147921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:30:18  来源:igfitidea点击:

ActionBarActivity getSupportActionBar().hide() throws NullPointerException

javaandroidnullpointerexceptionandroid-support-libraryandroid-actionbar-compat

提问by Abi-

Call

称呼

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

or just:

要不就:

getActionBar()

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

在 android.support.v7.app.ActionBarActivity 我得到这样的异常:

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

不返回空

回答by gunar

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

Theme.AppCompat.LightICS 之前和 ICS之后同时使用肯定会导致问题。这样做的方法是设置一个MyTheme

  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 Holotheme. Ex: <style name="MyTheme" parent="android:Theme.Holo.Light">
  1. values/styles.xml. 这将从Theme.AppCompat.Light. 前任:<style name="MyTheme" parent="Theme.AppCompat.Light">
  2. values-v11/styles.xml. 这将从一个Holo主题延伸。前任:<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.

这样,Android 就知道要根据运行时设备主机 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 methodException or something equivalent.

不要打电话getActionBar(),一旦你从延伸ActionBarActivity。在后 ICS 设备上,这将运行,但对于较低的 API 设备,您将获得运行时not supported method异常或类似的东西。

回答by Abi-

As I understand

我认为

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

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

不正确!因为getSupportActionBar()返回(android.support.v7.app.ActionBarImplICS)的非空实例

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

之后我们可以调用隐藏函数( getSupportActionBar().hide();)

but inside this function we will have NullPointerException because variable mActionBarinside 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

但是在这个函数里面我们会有 NullPointerException 因为变量mActionBar里面 android.support.v7.app.ActionBarImplICS 实例 == 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

据我了解在 android.support.v7.app.ActionBarImplICS 的构造函数中

 mActionBar = activity.getActionBar();

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

返回 null,因为我们的 Activity 没有通过 Theme 的 ActionBar

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

回答by ruidge

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

遇到同样的问题,但我使用代码在下面设置全屏和 noActionbar 而不是 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:

这段代码在ICS之前运行良好,但是由于ICS上面的NullPointException导致崩溃,经过一些实验,我得到了解决方案:删除一行没有设置标题的代码如下:

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

然后它在所有平台上都能很好地工作。:)

回答by Yar

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

简而言之: <item name="android:windowNoTitle">true</item>只能用于Activity,不可以ActionBarActivity

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

另一方面,getSupportActionBar().hide();可以与ActionBarActivity