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

You should add the Sherlock theme to your application.

Up vote 7 down vote favorite share g+ share fb share tw.

I'm trying to use the ActionBarSherlock library to provide backwards compatible ActionBar support with tabs in my Android app, so I downloaded the latest build, built the demo, and ran it. If you go to Action Bar, then select Tab Navigation it crashes 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 can't move forward with my app until this is fixed.

I wrote a bunch of code, set up the action bar in my app, and tried to run it, and it crashes with an NPE because of the null return value on the getSupportActionBar() call. The relevant 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 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) { } } android actionbar actionbarsherlock link|improve this question edited 09-03 at 3:59Jake Wharton4,2381045 asked 09-03 at 17:46Christopher Perry1,7871139 81% accept rate.

You need to provide some code. GetSupportActionBar() works in subclasses of FragmentActivity, at least after super.onCreate() is called. – Jon Willis Sep 3 '11 at 18:02 Code added.

The code is where it's crashing in the demo. The demo code is doing what you are saying, and it crashes with an NPE. It does the same thing in my app code, which is similar.

What I'm saying is, the call to getSupportActionBar always returns null, in my code, and in the demo provided with the library. Perhaps you are using a different version? I grabbed the latest version late last night.

– Christopher Perry Sep 3 '11 at 18:11 I'm using 3.1.3, downloaded as a zip from github.com/JakeWharton/ActionBarSherlock... and I do not get any crashes. If you downloaded from github, maybe there's a difference/bug. What version of the android SDK are you using?

– Jon Willis Sep 3 '11 at 20:01 I'm building against Android 3.2, which is required by the latest version of ActionBarSherlock. I'll try an earlier version of the latter and see if I have better luck. – Christopher Perry Sep 4 '11 at 4:38 I'm also using 3.1.3, as it turns out.

The demo crashes when trying to use tabbed navigation. – Christopher Perry Sep 4 '11 at 5:48.

2 I already had this as my theme, but I'll give you credit anyways. The real problem, I discovered, was that I was using Theme. NoTitleBar in the startup activity, which was the cause of the crash in my application.

I'm not sure what is causing the crash in the ActionBarSherlock demo, but my app works now so I'm moving on. – Christopher Perry Sep 5 '11 at 2:21 I somehow managed to omit adding the theme: android:theme="@style/Theme. Sherlock".

Thanks for your answer. – Igor Popov Dec 3 '11 at 13:47.

Another reason this will happen (on honeycomb +) devices is because the windowNoTitle attribute is set in your style. Get rid of that as ActionBarSherlock will automatically remove it in pre-honeycomb devices for you.

Thanks, this fixed the issue for me – Somatik Apr 2 at 16:12.

Another reason you might get null from getSupportActionBar() is trying to call it before setContentView(R.layout. Main) or in your example adding a fragment. I refactored oncreate and mistakenly put getSupportActionBar().

SetNavigationMode(ActionBar. NAVIGATION_MODE_TABS); after super. OnCreate(savedInstanceState).

Very helpful :) – Igor Popov Dec 3 '11 at 13:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions