解决getActionBar()报NullPointerException办法

来源:互联网 发布:逍遥安卓连不了网络 编辑:程序博客网 时间:2024/06/05 13:12

1. 加载完视图后,再去获取:

必须在setContentView()后面。

2.sdk版本:

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

[html] view plain copy
 print?
  1. <uses-sdk  
  2.        android:minSdkVersion="11"  
  3.        android:targetSdkVersion="17" />  

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

[html] view plain copy
 print?
  1. android:theme="@style/AppTheme" >  
其父主题要具有ActionBar,例如:

[html] view plain copy
 print?
  1. <style name="AppTheme" parent="@android:style/Theme.Holo.Light">  

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

[html] view plain copy
 print?
  1. //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  



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


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

values-v11中主题:

[html] view plain copy
 print?
  1. <resources>  
  2.   
  3.     <!--  
  4.         Base application theme for API 11+. This theme completely replaces  
  5.         AppBaseTheme from res/values/styles.xml on API 11+ devices.  
  6.     -->  
  7.     <style name="AppBaseTheme" parent="android:Theme.Holo.Light">  
  8.         <!-- API 11 theme customizations can go here. -->  
  9.     </style>  
  10.   
  11. </resources>  

所以,如果遇到报空的问题,你看看你的工程中有没有values-v11或者v14的包,没有的话就去其他工程中copy一份过来,改下就可以了
0 0
原创粉丝点击