getActionBar()在api 19中返回null,目标api 21分钟sdk 16

时间:2014-11-11 18:23:34

标签: android android-actionbar android-5.0-lollipop android-appcompat

我的应用程序在运行kitkat(api 19)的设备上崩溃,我尝试使用LG g3和nexus 7 2013因为nullPointer预期。

我发现当我执行getActionBar()时它返回null并且我无法修复它。

我使用appcompat主题21和运行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 个答案:

答案 0 :(得分:9)

如果您想在支持库上使用ActionBar,则必须扩展ActionBarActivity类而不是Activity,并使用getSupportActionBar()代替getActionBar()得到ActionBar

答案 1 :(得分:3)

我也遇到过这个问题。我刚刚删除了

  

values-v21 / styles.xml

然后它可以正常工作而不会发生任何崩溃。