0

我为我的抽屉创建了一个 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

4 回答 4

0

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

public abstract class BaseActivity extends Activity {
于 2013-12-15T11:53:17.227 回答
0

我遇到了这个问题,我是因为在抽象的 baseActivity 的 XML 布局中没有布局。只需为您的 xml 文件添加一个空的 LinearLayout 即可:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</LinearLayout>
于 2013-12-15T11:19:05.007 回答
0

如果您使用ActionBarActivitygetSupportActionBar()getActionBar().

于 2013-09-12T14:41:44.443 回答
0

在你的onCreate你没有任何setContentView(int layoutResource)方法,所以所有引用的视图都是空的,因为它们没有找到。

于 2013-09-30T16:34:02.760 回答