gpt4 book ai didi

java - android.app.Actionbar.setHomeButtonEnabled 返回 null

转载 作者:行者123 更新时间:2023-11-30 08:55:32 24 4
gpt4 key购买 nike

我正在尝试在 android 中创建可滑动的标签。甚至在 Activity 开始之前,应用程序就会崩溃,说 setHomeButtonenabled 返回空指针异常。

我的 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 和 target=21

最佳答案

如果您的 Activity 扩展了 ActionBarActivity,则更改

actionBar = getActionBar();

actionBar = getSupportedActionBar();

关于java - android.app.Actionbar.setHomeButtonEnabled 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com