在取ActionBar時候老是報nullpointerexception異常,在程序中打印以後也確實是null。在網上找了好多的資料,你們大部分人都認爲是SDK版本致使的,也確實有這個緣由。版本低於API level 11的SDK是沒法使用ActionBar的,只能使用ActionBarSherlock。可是我在Activity中 html
ActionBar ab = this.getActionBar(); System.out.println("A"+ab); ab.show();
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. java
須要設置應用的主題,如在AndroidManifest添加語句 android
<application android:theme="@android :style/Theme.Holo"> api
也就是說取不大ActionBar的願意有可能你是的SDK版本設置低於15,有多是沒有設置應用的主題。 app
解決方法: this
①把AndroidManifest中的android:minSdkVersion 或者 android:targetSdkVersion 屬性被設置成11或者更高 spa
②爲AndroidManifest中的application添加屬性 .net
android:theme="@android :style/Theme.Holo" code