hXT1022 hXT1022 - 6 months ago 33
Java Question

android.app.Actionbar.setHomeButtonEnabled returns null

I am trying to create swipeable tabs in android. Before even activity starts application crashessaying that setHomeButtonenabled returning nullpointerexception.

My onCreate method:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}

/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});


}


Here is my LOGCAT:

03-09 22:38:24.674: E/AndroidRuntime(1297): FATAL EXCEPTION: main
03-09 22:38:24.674: E/AndroidRuntime(1297): Process: com.appquest.prodactivity, PID: 1297
03-09 22:38:24.674: E/AndroidRuntime(1297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appquest.prodactivity/com.appquest.prodactivity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.os.Looper.loop(Looper.java:135)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-09 22:38:24.674: E/AndroidRuntime(1297): at java.lang.reflect.Method.invoke(Native Method)
03-09 22:38:24.674: E/AndroidRuntime(1297): at java.lang.reflect.Method.invoke(Method.java:372)
03-09 22:38:24.674: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-09 22:38:24.674: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-09 22:38:24.674: E/AndroidRuntime(1297): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
03-09 22:38:24.674: E/AndroidRuntime(1297): at com.appquest.prodactivity.MainActivity.onCreate(MainActivity.java:31)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.Activity.performCreate(Activity.java:5933)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-09 22:38:24.674: E/AndroidRuntime(1297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)


I am using minimumsdk=14 and target=21

Answer

If your activity extends ActionBarActivity then change

actionBar = getActionBar();

to

actionBar = getSupportedActionBar();