Android: getSupportActionBar () always returns null in the ActionBarSherlock library

I am trying to use the ActionBarSherlock library to ensure that ActionBar support is backwards compatible with tabs in my Android application, so I downloaded the latest build, built a demo, and started it.

If you go to the Action Bar, then select Tab Navigation, it will work every time. Here's the stack trace:

09-03 02:34:47.940: ERROR/AndroidRuntime(3078): FATAL EXCEPTION: main 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.demos/com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation}: java.lang.NullPointerException 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Handler.dispatchMessage(Handler.java:99) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:132) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.main(ActivityThread.java:4025) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invokeNative(Native Method) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invoke(Method.java:491) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): Caused by: java.lang.NullPointerException 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation.onCreate(ActionBarTabNavigation.java:19) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 09-03 02:34:47.940: ERROR/AndroidRuntime(3078): ... 11 more 

I cannot move forward with my application until this is fixed. I wrote a bunch of code, set up an action bar in my application and tried to launch it, and it crashes with NPE due to a null return value in the getSupportActionBar() call.

The corresponding code is actually in the demo for the library:

 public class ActionBarTabNavigation extends FragmentActivity implements ActionBar.TabListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportFragmentManager() .beginTransaction() .add(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(0)) .commit(); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); for (int i = 0; i < 3; i++) { ActionBar.Tab tab = getSupportActionBar().newTab(); tab.setText("Tab " + i); tab.setTabListener(this); getSupportActionBar().addTab(tab); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { getSupportFragmentManager() .beginTransaction() .replace(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(tab.getPosition())) .commit(); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } } 
+65
android actionbarsherlock android-actionbar
Sep 03 2018-11-11T00:
source share
14 answers

You must add a Sherlock theme to your application.

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="false" android:theme="@style/Theme.Sherlock"> 
+81
Sep 04 '11 at 17:23
source share

I had the same issue on Android ICS 4.0.4. I used requestWindowFeature(Window.FEATURE_NO_TITLE); in FragmentActivity, but that hid the ActionBar on ICS + devices, which caused getSupportActionBar() be null.

Just remove:
requestWindowFeature(Window.FEATURE_NO_TITLE);

And it worked like a charm.

Hope this helps someone.

+62
Apr 12 '12 at 13:30
source share

Another reason this happens on Honeycomb + devices is because the windowNoTitle attribute windowNoTitle set in your style. Get rid of this, as ActionBarSherlock will automatically delete it in pre-cell devices for you.

+37
Dec 12 '11 at 19:05
source share

Another reason you can get null from getSupportActionBar() is trying to call it before setContentView(R.layout.main) or add a fragment in your example.

I reorganized oncreate and mistakenly set getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); after super.onCreate(savedInstanceState);

+14
Nov 26 '11 at 16:24
source share

That's funny: don't ask a topic -

  android:theme="@style/Theme.NoActionbar" 
+9
Sep 19 '13 at 20:26
source share

Another reason you can get null from getSupportActionBar() is when activity is used in TabHost on Honeycomb +.

+6
Jul 11 2018-12-12T00:
source share

Depending on how you write out the code. make sure you first install the toolbar before invoking it.

  mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(true); 
+5
Aug 08 '15 at 4:10
source share

I just changed in the manifest

 android:theme="@style/AppTheme.NoActionBar" 

at

  android:theme="@style/AppTheme" 

and the error has passed

+2
Dec 08 '16 at 9:31
source share

whenever we set a custom view using the sherlock library. just delete this request WindowFeature (Window.FEATURE_NO_TITLE); how we do customview using the sherlock bar library.

  getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.header_sherlock_xmllayout); header_tvleft = (TextView) findViewById(R.id.header_tvleft); header_tvleft.setText("Back"); 
+1
Jul 10 '13 at 10:57
source share

I had this problem after (in absentia) forgetting to call super.onCreate()

+1
Jan 24 '14 at 20:41
source share

you are declared by Theme.Sherlock or Theme.Sherlock.Light as your activity or application theme in the manifest or using a custom theme that inherits from one of these two

Example: -

 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Sherlock.Light" > 

You can also use a dark theme: -

 android:theme="@style/Theme.Sherlock" 
+1
May 21 '14 at 6:57
source share

I came across this after adding a library to my project. The tool was to look in the library and delete any styles with the name "AppTheme" if you use the same theme name in your manifest. There was no conflict with my Galaxy S4, Bean Jelly, while a conflict arose on my Galaxy tab.

0
Jan 20 '14 at 17:10
source share

I added android:theme="@android:style/Theme.Dialog" to my activity in the Android manifest file to make it interactive. This will also remove the action bar and therefore the null pointer. Delete it or do not call getSupportActioBar

0
Aug 13 '15 at 10:13
source share

I am making this mistake because I am adding toobar in the xml fragment file. The code to find the toolbar in the snippet is: getActivity (). FindViewByid (id ...), but my toolbar is in the xml fragment file, so there was no toolbar found and no setSipprotActionBar () toolbar, and also nothing to get when getSupportActionBar (). Therefore, remember: Do not put the toolbar in the xml file of your fragment.

0
Sep 16 '16 at 13:03
source share



All Articles