在取ActionBar时候总是报nullpointerexception异常,在程序中打印之后也确实是null。在网上找了好多的资料,大家大部分人都认为是SDK版本导致的,也确实有这个原因。版本低于API level 11的SDK是无法使用ActionBar的,只能使用ActionBarSherlock。但是我在Activity中
ActionBar ab = this.getActionBar();
System.out.println("A"+ab);
ab.show();
打印的结果仍然为null,这是怎么回事呢?查了API后才知道:
Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default.
需要设置应用的主题,如在AndroidManifest添加语句
<application android:theme="@android :style/Theme.Holo">
也就是说取不大ActionBar的愿意有可能你是的SDK版本设置低于15,有可能是没有设置应用的主题。
解决方法:
①把AndroidManifest中的android:minSdkVersion 或者 android:targetSdkVersion 属性被设置成11或者更高
②为AndroidManifest中的application添加属性
android:theme="@android :style/Theme.Holo"