问题描述
呼叫
if (getSupportActionBar() != null)
getSupportActionBar().hide();
或者只是:
getActionBar()
在android.support.v7.app.ActionBarActivity我得到这样的例外:
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)
...
编辑:它只是发生在有活动的主题:
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>
请注意:
getSupportActionBar()
不返回null
do not return null
推荐答案
满足同样的问题,但我用code设置全屏和noActionbar下方,而不是主题的xml:
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();
}
这code运行良好ICS之前,但crashs上述ICS造成NullPointException,一些实验后,我得到了解决:删除一行code的设置没有标题如下:
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. : )
这篇关于ActionBarActivity getSupportActionBar()。隐藏()抛出NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!