Objective
In the use of ActionBar, sometimes there will be a null pointer exception, this is because the application did not get to ActionBar caused, and the application did not get to ActionBar the reasons for more, so we will summarize below the ActionBar can not get the cause of the error.
Solutions
- Check to see if your app is set without ActionBar theme , or if you set the Noactionbar or Notitlebar properties directly, check these points: check The theme property in the tag in Androidmanifest.xml, and then checks to see if there are different theme in the Activity tab causing the interface ActionBar cannot be displayed,
@android: Style/theme.notitlebar and <item name= "Windowactionbar" >false</ item>
<item name= "Android:windownotitle" >TRUE</ITEM> such properties will make the Actionbar in your app disappear, remember, If you want to use Actionbar, be sure to clear out these theme property settings, if your project in the Res directory in addition to the values directory, there are other res/values-v11,style/values-v14, etc., the directory Styles.xml need to check, and finally check whether the activity code is dynamically set full screen or do not display titlebar, the code is as follows:
//requestwindowfeature (window.feature _no_title);
//getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_ fullscreen);
The above two lines of code, the above sentence set activity does not display titlebar, the next sentence set activity to full-screen mode, both code will cause ActionBar
can not display, so that Getactionbar () gets failed, and if activity in your project has activity at the parent level, you also need to comment out the notitle and full-screen code in the activity at the parent level.
- If you are unsure whether the theme Theme your app supports ActionBar, you can set it to Theme.appcompatin the Res/values.xml file, which is AppCompat theme in Theme. ActionBar can also be used on low-version Android (note: res/values,res/values-v11,res/ The style in a directory such as VALUES-V14 is set to Theme.appcompat, and if the settings are different, it may cause some versions to use Theme without ActionBar, and acticity in ActionBar (Getactionbar () or Getsupportbar ()), which needs to be called after Setcontentview (R.layout.main);
- If your project is using Android.app.ActionBar, you'll need to change the minimum supported version of your app to one (Android 3.0), use Getactionbar () to get ActionBar, If your project supports Android 3.0 or less, your project needs to introduce the latest AppCompat Compatibility Pack and then change the ActionBar reference to Android.support.v7.app.ActionBar , and use Getsupportactionbar to obtain ActionBar;
- In addition to the above reasons may cause ActionBar to be empty, there are other reasons for Getactionbar () failed to obtain, for example, when I look online, someone's project was created on Android SDK 2.2, when he used the ActionBar will be The minsdkversion in Androidmanifest.xml is set to 11 (), and the Discovery Getactionbar () gets failed because there is no res/values-v11 and res/in the project Values-v14 and one of the style.xml files that I recently encountered with Getactionbar () is null to import a project that someone else sent me in Eclipse, but to compile the run apk Getactionbar on his computer, But on my computer is not, try the above several methods also not, finally, I put this project into the Android Studio, finally can run normally.
If you have other questions about Getactionbar () null, methods and experiences, please discuss the exchange.
Reference links
Http://vcsos.com/Article/pageSource/150125/20150125093506.shtml
http://stackoverflow.com/questions/27959107/getting-the-null-pointer-exception-in-the-getactionbar-method/27960390
Http://www.apkbus.com/android-130755-1-1.html
Http://www.cnblogs.com/shortboy/archive/2013/04/18/3029029.html
Http://stackoverflow.com/questions/6867076/getactionbar-returns-null
http://blog.csdn.net/yueqinglkong/article/details/39577749
Http://stackoverflow.com/questions/8499181/actionbar-throws-nullpointerexception
Http://stackoverflow.com/questions/28255552/nullpointerexception-caused-by-getactionbar
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Summary of Workarounds for "Android" Getactionbar () null