简体   繁体   English

Android导航抽屉BaseActivity

[英]Android Navigation Drawer BaseActivity

I've created a BaseActivity for my Drawer but it always crashes on start. 我已经为我的抽屉创建了一个BaseActivity,但是它总是在启动时崩溃。 I don't know why it alsways crashes. 我不知道为什么它总是崩溃。 Does anybody know what to do or how to configure the BaseActivity right? 有人知道该怎么做或如何配置BaseActivity吗?

It always crashes in the onCreate and when I configure the drawer. 在onCreate和配置抽屉时,它总是崩溃。 Any ideas? 有任何想法吗?

Code of the BaseActivity : 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);
    }
}

如果您使用的是ActionBarActivity使用getActionBar() getSupportActionBar()方法。

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

I had this issue and i was due to not having a layout in abstracted baseActivity's XML layout. 我遇到了这个问题,原因是抽象的baseActivity的XML布局中没有布局。 Just add an empty LinearLayout for instanse to your xml file: 只需向您的xml文件添加一个用于实例的空LinearLayout:

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

You are extending ActionBarActivty. 您正在扩展ActionBarActivty。 Try extending Activity class only. 尝试仅扩展Activity类。

public abstract class BaseActivity extends Activity {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM