我有Navigation Drawer
菜单,因为我有片段Frag1,Frag2和Frag3。
Frag2被Frag21替换,Frag21被Frag22替换,等等
如果我按下Frag22中的按钮到Frag21和Frag2,应用程序将正常关闭
但是,如果我直接从Frag22转到Frag1并尝试关闭应用程序,它会崩溃并且我收到错误
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f0f00c4 (com.NuSS.MyPAS:id/root_frame) for fragment CategoriesDisplayList{4260ca00 #1 id=0x7f0f00c4}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:960)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1553)
at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:497)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)**
还给出了
@Override
public void onBackPressed() {
if(fragmentManager.getBackStackEntryCount() != 0) {
fragmentManager.popBackStack();
} else {
super.onBackPressed();
}
}
在MainFragment Activity中。
请让解决方案在几天内解决此错误。
更新
代码段
RootFragment
public class RootFragment extends Fragment {
private static final String TAG = "RootFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.activity_root_fragment, container, false);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
/*
* When this container fragment is created, we fill it with our first
* "real" fragment
*/
transaction.replace(R.id.root_frame, new CategoriesDisplayList());
transaction.commit();
return view;
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
try {
Fragment fragment = (getFragmentManager()
.findFragmentById(R.id.root_frame));
FragmentTransaction ft = getActivity().getSupportFragmentManager()
.beginTransaction();
ft.remove(fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
CategoryDisplayList
public class CategoriesDisplayList extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.root_frame, new RootFragmentService());
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.addToBackStack(null);
trans.commit();
}
});
//registerForContextMenu(vendorListView);
return rootView;
}
RootFragmentService
public class RootFragmentService extends Fragment {
private static final String TAG = "RootFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_root_fragment, container, false);
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
/*
* When this container fragment is created, we fill it with our first
* "real" fragment
*/
transaction.replace(R.id.root_frame, new ServicesList());
transaction.commit();
return view;
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
try {
Fragment fragment = (getFragmentManager()
.findFragmentById(R.id.root_frame));
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.remove(fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
提前致谢。
答案 0 :(得分:0)
在Android开发人员的后台按钮中创建onClickButton监听器更多信息:View.OnClickListener
答案 1 :(得分:0)
如果您想返回堆栈,则无需在popBackStack
事件上调用onBackPressed
。如果您在transAction上使用addToBackStack
方法,则onBackPressed会执行此操作而无需额外的代码。
fragmentManager.beginTransaction().setCustomAnimations(animationIn, animationOut).replace(layout, fragment, tag).addToBackStack(backStackTag).commit();