在API 21,目标SDK为16的情况下,getActionBar()返回null

3

我的应用在运行kitkat(api 19)设备时崩溃,我试过在LG G3和2013年的nexus 7上尝试,因为出现了nullPointer异常。

我发现当我调用getActionBar()时,它返回null,我无法修复它。

我使用appcompat 21主题和material主题来兼容运行lollipop的设备,在这些设备上应用程序可以正常工作。

MainActivity.java:

package com.sqvat.betterpowermat;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {

    private ActionBar actionBar;

    private String[] categories;
    private DrawerLayout drawerLayout;
    private ListView drawerList;
    private ActionBarDrawerToggle drawerToggle;

    private String drawerClose;
    private String drawerOpen;

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

        actionBar = getActionBar();

        //Nav Drawer
        categories = getResources().getStringArray(R.array.categories);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.left_drawer);

        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.nav_drawer_li, R.id.drawer_li_textview, categories));

        drawerList.setOnItemClickListener(new DrawerItemClickListener());

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawerOpen, R.string.drawerClose) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        // Set the drawer toggle as the DrawerListener
        drawerLayout.setDrawerListener(drawerToggle);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        selectItem(0);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (position){
            case 0:
                fragmentTransaction.replace(R.id.content_frame, new HomeFragment());
                actionBar.setTitle("Home");
                break;
            case 1:
                fragmentTransaction.replace(R.id.content_frame, new LocationsFragment());
                actionBar.setTitle("Locations");
                break;
            case 2:
                fragmentTransaction.replace(R.id.content_frame, new StoreFragment());
                actionBar.setTitle("Store");
                break;
            case 3:
                fragmentTransaction.replace(R.id.content_frame, new MoreFragment());
                actionBar.setTitle("More");
                break;




        }
        fragmentTransaction.commit();

        drawerList.setItemChecked(position, true);
        drawerLayout.closeDrawer(drawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的appcompat样式(values/styles.xml):

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

    </style>

    <color name="colorPrimary">#4CAF50</color>
    <color name="colorPrimaryDark">#43A047</color>

我的Android棒棒糖样式(values-v21/styles.xml):

<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
2个回答

9
如果您想在支持库中使用ActionBar,则需要扩展ActionBarActivity类而不是Activity类,并使用getSupportActionBar()代替getActionBar()来获取ActionBar

我忘记写我的最小SDK是16,我不需要支持操作栏,我只需要支持材料主题。有没有办法使用appcompat主题和常规操作栏? - user3629774
不,Material主题仅适用于SDK 21。您需要使用AppCompat库才能在API低于21的情况下获得Material主题外观。 - jimmy0251
有没有办法在常规操作栏中使用appcompat主题? - user3629774

3

我也遇到了这个问题。 我只是删除了

values-v21/styles.xml

然后它就可以正常工作了,没有崩溃。


AppCompat主题无法正常工作。 我尝试使用“android:Theme.Holo.Light”进行替换, 结果成功了! - Phuong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接