在方向改变后,Android没有为片段找到视图

时间:2014-08-01 00:06:09

标签: android android-fragments

我的设备方向发生变化时出错。一切正常,直到方向改变。

我有一个片段(TrapCheckContainerFragment),其中包含一个FrameLayout,用于存放另一个片段(TrapCheckFragment)。 TrapCheckFragment是在TrapCheckContainerFragment的onCreateView方法中创建的,并使用FragmentTransaction放入FrameLayout。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_trap_check_container, container, false);

    TrapCheckFragment fragment = new TrapCheckFragment();

    ReplaceFragment(R.id.trapCheckHolder, fragment, false, feature.trapUniqueID.toString());

    return rootView;
}

private void ReplaceFragment(int idToReplace, Fragment newFragment, boolean addToBackStack, String tag){

    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.replace(idToReplace, newFragment, tag);

    if(addToBackStack){
        transaction.addToBackStack(null);
    }

    transaction.commit();
}

这一切都正常,直到我旋转设备。据我所知,我的代码都没有被调用,但由于某种原因,FragmentManager无法找到名为trapCheckHolder的视图,这是用于保存子片段的FrameLayout。如果我删除对ReplaceFragment的调用,那么当我旋转设备时一切正常。

logcat消息:

 FATAL EXCEPTION: main
 Process: com.redacted.appname, PID: 29396
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.redacted.appname/com.redacted.appname.activities.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0a0042 (com.redacted.appname:id/trapCheckHolder) for fragment TrapCheckFragment{64fa6b18 #4 id=0x7f0a0042 10236}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3724)
    at android.app.ActivityThread.access$900(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0a0042 (com.redacted.appname:id/trapCheckHolder) for fragment TrapCheckFragment{64fa6b18 #4 id=0x7f0a0042 10236}
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    at android.app.Activity.performStart(Activity.java:5240)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
    ... 10 more

我已尝试将setRetainInstance设置为true和false,以及父片段,但这并没有任何区别。我在这里做的事情有什么明显的错误吗?有没有办法强制创建一个新的TrapCheckFragment,而不是试图显示似乎导致此问题的旧版本?

如果需要,我可以提供有关任何代码或错误的更多信息。

1 个答案:

答案 0 :(得分:1)

似乎onCreateViewMethod导致了问题;如果容器为null,它必须返回null:

执行类似的操作以检查null并重新创建视图:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (rootView == null) {
            // Inflate the layout for this fragment 
            rootView = inflater.inflate(R.layout.fragment_a, container, false);

        } else { 
            // Do not inflate the layout again. 
            // The returned View of onCreateView will be added into the fragment. 
            // However it is not allowed to be added twice even if the parent is same. 
            // So we must remove rootView from the existing parent view group 
            // (it will be added back). 
            ((ViewGroup)rootView.getParent()).removeView(rootView);
        } 
        return rootView;

请检查,如果有效,请告诉我。