Skip to main content

Android: getSupportActionBar() always returns null in ActionBarSherlock library


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 < 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) {
}
}


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You should add the Sherlock theme to your application

    <application android:icon="@drawable/icon" android:label="@string/app_name"
    android:debuggable="false" android:theme="@style/Theme.Sherlock">

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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);

    ReplyDelete
  4. I had the same problem on the Android ICS 4.0.4. I was using requestWindowFeature(Window.FEATURE_NO_TITLE); on the FragmentActivity, but this was hiding the ActionBar on ICS+ devices that caused the getSupportActionBar() to be null.

    Simply removed the:
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    And it worked like a charm.

    Hope it helps someone.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.