我有一个Activity
,每边有两个NavigationDrawer
个。我想清理它并将两个菜单拉出到自己的xml文件中,并使用include在主xml中引用它们。这适用于左侧抽屉,但不适用于右侧。
<include
android:id="@+id/friendsInclude"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/friend_feed"
android:layout_gravity="start"/>
<!-- Notification Feed -->
<include
android:id="@+id/notificationInclude"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/notification_feed"
android:layout_gravity="end" />
两者都有自己的XML,根布局是相对布局。它们看起来像这样......
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bootstrapbutton="http://schemas.android.com/apk/res-auto"
xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
xmlns:bootstrapthumbnail="http://schemas.android.com/apk/res-auto"
android:id="@+id/friendsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#F5F5F5">
和
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bootstrapbutton="http://schemas.android.com/apk/res-auto"
xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
xmlns:bootstrapthumbnail="http://schemas.android.com/apk/res-auto"
android:id="@+id/notificationsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#F5F5F5" >
在我们的主要活动中,我们在尝试通过操作栏打开右侧抽屉时获得空指针。
@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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if(item.getItemId() == R.id.notifications) {
if(drawerLayout.isDrawerVisible(notificationsLayout)) { //nullpointer
drawerLayout.closeDrawer(notificationsLayout);
} else {
drawerLayout.openDrawer(notificationsLayout);
drawerLayout.closeDrawer(friendsLayout);
}
}
if(item.getItemId() == R.id.refresh) {
new AsyncMainFeed(this).execute("");
new AsyncFriendFeed().execute("");
new AsyncNotificationFeed(this).execute("");
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
我宣布我的布局是这样的
friendsLayout = (RelativeLayout)findViewById(R.id.friendsLayout);
notificationsLayout = (RelativeLayout)findViewById(R.id.notificationsLayout);
我有点难过这是来自任何想法的地方?