getSupportActionBar 在android 4.1及更高版本中返回null。我正在使用支持v4和 SherlockFragmentActivity 。
这是我的logcat。
03-10 11:35:47.876: E/Crittercism(4778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cx.android/com.cx.android.ui.HomeActivity}: java.lang.NullPointerException
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-10 11:35:47.876: E/Crittercism(4778): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 11:35:47.876: E/Crittercism(4778): at android.os.Looper.loop(Looper.java:137)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-10 11:35:47.876: E/Crittercism(4778): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 11:35:47.876: E/Crittercism(4778): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 11:35:47.876: E/Crittercism(4778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-10 11:35:47.876: E/Crittercism(4778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-10 11:35:47.876: E/Crittercism(4778): at dalvik.system.NativeStart.main(Native Method)
03-10 11:35:47.876: E/Crittercism(4778): Caused by: java.lang.NullPointerException
03-10 11:35:47.876: E/Crittercism(4778): at com.cx.android.ui.HomeActivity.onCreate(HomeActivity.java:71)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.Activity.performCreate(Activity.java:5283)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-10 11:35:47.876: E/Crittercism(4778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-10 11:35:47.876: E/Crittercism(4778): ... 11 more
这是我的编辑。 该课程返回NPE。
public class HomeActivity extends SherlockFragmentActivity implements OnNavigationListener {
public static final String TAG = "HomeActivity";
private HomeDropdownListAdapter homeAdapter;
private FragmentManager fragmentManager;
private DashboardFragment dashboard;
private ActionBar actionBar;
private static HomeActivity staticInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
staticInstance = this;
fragmentManager = getSupportFragmentManager();
navStack = new Stack<Long>();
// Action Bar
actionBar = HomeActivity.this.getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//actionBar.setDisplayOptions(10);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
homeAdapter = new HomeDropdownListAdapter(this);
actionBar.setListNavigationCallbacks(homeAdapter, this);
actionBar.setSelectedNavigationItem(App.STARTING_NAV_INDEX);
编辑2: 添加了我的清单主题: 这是从我的应用程序标签的主题调用:
<style name="Theme.MainTheme" parent="@style/Theme.Sherlock">
<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="android:windowBackground">@color/background</item>
<item name="actionBarSize">54dp</item>
<item name="android:actionBarSize">54dp</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
<item name="background">@drawable/actionbar_background</item>
<item name="android:background">@drawable/actionbar_background</item>
</style>
<style name="Theme.Dialog" parent="@android:style/Theme.Dialog">
<item name ="android:windowActionBar">false</item>
</style>