gpt4 book ai didi

java - android 尝试在空对象引用上调用虚拟方法 'void android.app.ActionBar.setHomeButtonEnabled(boolean)'

转载 作者:行者123 更新时间:2023-12-01 22:09:34 25 4
gpt4 key购买 nike

我有代码,我正在通过导入项目及其运行在 Eclipse 上尝试

但是当我在 android studio 上尝试时遇到问题,这是:

Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object referenc

我使用 android.support.v4 ,这是代码:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

@Override
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) {
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu);
return true;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

我现在能做什么?我是 Android 编程新手吗?

请评估我并编辑我的代码以在我的项目中运行

最佳答案

getActionBar() 替换为 getSupportActionBar()。将 import android.app.ActionBar; 替换为 import android.support.v7.app.ActionBar;您遇到的另一个错误:您将 android.support.v4.app.FragmentActivityandroid.app.FragmentTransaction; 您应该将 import android.app.FragmentTransaction; 替换为 import android.support.v4.app.FragmentTransaction; 相同import android.app.ActionBar.Tab;

的东西

关于java - android 尝试在空对象引用上调用虚拟方法 'void android.app.ActionBar.setHomeButtonEnabled(boolean)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080890/

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