我正在尝试在两个片段之间实现导航。一个是我的应用程序的默认视图,它只包含带有自己项目的RecyclerView,另一个是带有TableLayout作为子项的ScrollView。应用正确地从我的ViewAdverts
片段切换到AddAdvert
,但是当我想要返回时,会出现异常和应用崩溃:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.polsl.elektr.quicktrade011, PID: 14676
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:248)
at android.support.design.widget.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:444)
at android.support.design.widget.BaseTransientBottomBar$1.handleMessage(BaseTransientBottomBar.java:185)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我不知道为什么只有当从ScrollView返回到RecyclerView时才出现该错误,而当默认片段包含RecyclerView时,不会在app启动时出现。
以下是我的.java
和.xml
文件:
5 files at Gist
答案 0 :(得分:0)
片段A
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragment = new DashboardFragment();
fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null).commit();
片段B
rootview.setFocusableInTouchMode(true);
rootview.requestFocus();
rootview.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
// DO WHAT YOU WANT ON BACK PRESSED
getFragmentManager().popBackStack();
return true;
} else {
return false;
}
}
});