从support-v4更改为appcompat-v7时出现NulledPointer异常

时间:2015-01-12 22:22:33

标签: java android android-activity android-fragments

我的应用使用com.android.support:support-v4:21.0.3使用Theme.Holo.light。现在我将我的应用主题更改为Theme.Appcompat,并从" com.android.support更改了gradle依赖项:support-v4:21.0.3" to" com.android.support:appcompat-v7:21.0.3"。 完成这些更改后,每当我启动它时,我的应用程序都会崩溃。 并获取日志

  

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)        引起:java.lang.NullPointerException:尝试调用虚方法' void   android.app.ActionBar.setHomeButtonEnabled(布尔值)'在null对象上   参考               在com.viter.android.navigationdrawerexample.MainActivity.onCreate(MainActivity.java:99)

我的功能是

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

    //Code for Tabs
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    // Set up the action bar.
    final ActionBar actionBar = getActionBar();


    actionBar.setHomeButtonEnabled(false);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }


    // Navigation Drawer
    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

您应该使用getSupportActionBar()代替getActionBar()(并且也可以使用ActionBarActivity代替FragmentActivity)。

答案 1 :(得分:1)

使用AppCompat v21,您可以添加工具栏并将其用作ActionBar。这并不难,大多数代码都不需要更改。主要是将工具栏添加到activity_main.xml

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

然后使用它来制作你的Actionbar

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();

}

根据您的其余代码,可能还需要更新其他一些内容。但这应该可以帮到你。更多详情:https://chris.banes.me/2014/10/17/appcompat-v21/

答案 2 :(得分:0)

检查你的

final ActionBar actionBar = getActionBar();

为actionBar返回null。

您可能在错误的班级中调用代码。