有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓试图调用虚拟方法“void 安卓”。应用程序。ActionBar。空对象引用上的setHomeButtonEnabled(布尔)“”

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

但当我在安卓 studio上试用时,我遇到了问题,这是:

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

我正在使用安卓。支持v4,这是代码:

import 安卓.app.ActionBar;
import 安卓.app.ActionBar.Tab;
import 安卓.app.FragmentTransaction;
import 安卓.os.Bundle;
import 安卓.support.v4.app.FragmentActivity;
import 安卓.support.v4.view.ViewPager;
import 安卓.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) {
}

}

我现在能做什么?我喜欢安卓编程吗

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


共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    尝试获取操作栏:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // The Action Bar is a window feature. The feature must be requested
        // before setting a content view. Normally this is set automatically
        // by your Activity's theme in your manifest. The provided system
        // theme Theme.WithActionBar enables this for you. Use it as you would
        // use Theme.NoTitleBar. You can add an Action Bar to your own themes
        // by adding the element <item name="android:windowActionBar">true</item>
        // to your style definition.
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    
        setContentView(R.layout.activity_main);
    
        actionBar = getActionBar();
    
    [...]
    }