android.app.Actionbar.setHomeButtonEnabled возвращает ноль
Я пытаюсь создать пролистываемые вкладки в Android. Еще до начала активности приложения происходит сбой, сообщая, что setHomeButtonenabled возвращает nullpointerexception.
Мой метод onCreate:
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) {
}
});
}
Вот мой 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)
Я использую MinimumSDK =14 и цель =21
3 ответа
Решение
Если ваша деятельность расширяет ActionBarActivity, измените
actionBar = getActionBar();
в
actionBar = getSupportedActionBar();
** изменить в теме файла AndroidManifest на @android:style/Theme.Holo.Light
Измените тему своей деятельности следующим образом: android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge"
это работает для меня.