我的活动已扩展到AppCompatActivity。我也成功设置了一个抽屉。现在,当我对Fragment进行交易时,基本上它使用了begintransaction。但是我在Unreachable statement
上收到了此错误消息android.app.FragmentManager fragmentManager = getFragmentManager();
,我也尝试了android.support.v4.app包,它也说这是废弃的。
在我的抽屉导航onClickListener
我有这个
`
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
android.app.Fragment objFragment = null;
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.dhome:
objFragment = new panel();
mCurrentSelectedPosition = 0;
return true;
case R.id.dsetting:
mCurrentSelectedPosition = 1;
return true;
case R.id.dlogout:
mCurrentSelectedPosition = 2;
return true;
default:
return true;
}
// update the main content by replacing fragments
if(objFragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}
}
});`
在我的布局中
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
>
<FrameLayout android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#d9d8d8"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
使用此:
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.dhome:
objFragment = new panel();
mCurrentSelectedPosition = 0;
break;
case R.id.dsetting:
mCurrentSelectedPosition = 1;
break;
case R.id.dlogout:
mCurrentSelectedPosition = 2;
break;
}
// update the main content by replacing fragments
if(objFragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}
return true;
}
您之前的代码没有考虑用户如何与actionbar
(点击任何项目)进行互动,return true;
将始终在到达片段事务代码之前执行