Android导航抽屉BaseActivity

时间:2013-09-12 14:14:28

标签: android navigation-drawer

我为抽屉创建了一个BaseActivity但它在启动时总是崩溃。我不知道为什么它会崩溃。有人知道该做什么或如何配置BaseActivity吗?

它总是在onCreate和我配置抽屉时崩溃。有什么想法吗?

BaseActivity的代码:

    public abstract class BaseActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    public Context context;

    @Override
    public void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        String[] menu = getResources().getStringArray(R.array.menu);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menu));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_navigation_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) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

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

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
       if (mDrawerToggle.onOptionsItemSelected(item)) {
           return true;
       }
    return true;
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {


            case 0:
                Intent all = new Intent(context, All.class);
                startActivity(all);
                break;
            case 1:
                Intent country = new Intent(context, ByCountry.class);
                startActivity(country);
                break;
            case 2:
                Intent device = new Intent(context, ByDevice.class);
                startActivity(device);
                break;
            case 3:
                Intent about = new Intent(context, About.class);
                startActivity(about);
                break;
            case 4:
                Intent info = new Intent(context, Info.class);
                startActivity(info);
                break;
            case 5:
                Intent settings = new Intent(context, Settings.class);
                startActivity(settings);
                break;
            }
        } };


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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

4 个答案:

答案 0 :(得分:0)

如果您使用ActionBarActivity使用getSupportActionBar()而不是getActionBar()

答案 1 :(得分:0)

onCreate中,您没有任何setContentView(int layoutResource)方法,因此所有引用的视图都为空,因为找不到它们。

答案 2 :(得分:0)

我遇到了这个问题,而且由于在抽象的baseActivity的XML布局中没有布局。 只需为您的xml文件添加一个空的LinearLayout for instanse:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</LinearLayout>

答案 3 :(得分:0)

您正在扩展ActionBarActivty。尝试仅扩展Activity类。

public abstract class BaseActivity extends Activity {