只有当我启动此片段(第一次)并同时按下后退按钮时,应用程序才会崩溃。我没有在我的活动中覆盖onBackPressed()。
代码:
片段类
public class Fragment_View extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentview, container, false);
root_view = view;
setupExtraViews();
return view;
}
private void setupExtraViews() {
if (root_view.findViewById(R.id.someview) != null) {
Fragment_Top_Bar top_bar = new Fragment_Top_Bar();
//TODO: app crashes here on the .commit part when this fragment is started and back button is pressed at the same time.
getChildFragmentManager().beginTransaction().
add(R.id.someview, top_bar).commit();
}
}
...
} 例外是:
E/InputEventSender﹕ Exception dispatching finished signal.
E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI﹕ java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1387)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
at in.com.xxxxxx.main_app.settings.Fragment_Class.setupExtraViews(Fragment_Class.java:45)
at in.com.xxxxxx.main_app.settings.Fragment_Class.onCreateView(Fragment_Class.java:24)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:501)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:176)
at android.app.Activity.onKeyUp(Activity.java:2274)
at android.view.KeyEvent.dispatch(KeyEvent.java:2985)
at android.app.Activity.dispatchKeyEvent(Activity.java:2508)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2174)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4505)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4472)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4033)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4171)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4064)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4228)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4033)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4064)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4033)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4204)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4364)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2257)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1907)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1898)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2234)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
然后再次显示完全相同的错误。因此,在创建片段时按下后退按钮时应用程序崩溃,然后此异常显示两次。我认为.commit()是异步的。所以我认为,在调度程序实际提交的时间和调度程序获得提交的时间之间,onBackPressed正在破坏活动。因此错误。我不确定这是实际的原因。所以请帮忙......