您的位置:首页 > 其它

解决getActionBar()报NullPointerException办法

2017-04-19 23:57 302 查看
1. 加载完视图后,再去获取:

必须在setContentView()后面。

2.sdk版本:

Actionbar的主题在3.0以后才有,使用的时候要确保,最低的版本不能小于3.0。

[html] view
plain copy

 print?

<uses-sdk  

       android:minSdkVersion="11"  

       android:targetSdkVersion="17" />  

3.
(下面这俩一定要)配置清单中,添加的主题:

[html] view
plain copy

 print?

android:theme="@style/AppTheme" >  

其父主题要具有ActionBar,例如:

[html] view
plain copy

 print?

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">  

4.活动类及其父类中,不要设置为全屏:

[html] view
plain copy

 print?

//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  

以上基本可解决, 若尚未解决, 再试试下面方法:

5.没有引用到v11+包中的主题。



values-v11中主题:

[html] view
plain copy

 print?

<resources>  

  

    <!--  

        Base application theme for API 11+. This theme completely replaces  

        AppBaseTheme from res/values/styles.xml on API 11+ devices.  

    -->  

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">  

        <!-- API 11 theme customizations can go here. -->  

    </style>  

  

</resources>  



所以,如果遇到报空的问题,你看看你的工程中有没有values-v11或者v14的包,没有的话就去其他工程中copy一份过来,改下就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐