导航抽屉不适用于扩展活动

时间:2014-07-22 10:30:08

标签: android android-activity navigation-drawer

我在BaseActivity中实现了NavigationDrawer。这是我的代码,

public class BaseActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_activity);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    drawerListItems = new ArrayList<DrawerItem>();

    // Add Drawer Item to dataList
    drawerListItems.add(new DrawerItem("Profile", R.drawable.profile));
    drawerListItems.add(new DrawerItem("My bookings", R.drawable.mybookings));
    drawerListItems.add(new DrawerItem("Settings", R.drawable.settings));
    drawerListItems.add(new DrawerItem("Messages", R.drawable.messages));


    custadapter = new CustomDrawerAdapter(this, R.layout.custom_draweritem,
            drawerListItems);

    mDrawerList.setAdapter(custadapter);


    // Enabling Home button
            getActionBar().setHomeButtonEnabled(true);

            // Enabling Up navigation
            getActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when drawer is closed */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("");
            invalidateOptionsMenu();

        }

        /** Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("");
            invalidateOptionsMenu();
        }



    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);




    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


            if (position == 0) {
                processProfileActivity();

            }
            if (position == 1) {
                processmyBookin();

            }

            if (position == 2) {
                processSettings();

            }
             if (position == 3) {
                processMessage();

            }



            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
  }

然后我扩展BaseActivty的另一个活动是

public class SecondActivity extends BaseActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vehicledetails);
 }

我的BaseActivity的xml文件是

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<FrameLayout
    android:id="@+id/containerMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

问题在于我的第二个活动NavigationDrawer没有出来。不知道我错过了什么。如果有人帮我,我会很感激。

也没有注销任何错误,只是当我点击导航图标时,它没有在SecondActivity中打开。

1 个答案:

答案 0 :(得分:0)

将您的代码更改为此

public class BaseActivity extends AppCompatActivity {}

您没有在BaseActivity中扩展AppCompatActivity。