1

我里面有多个片段MainActivity我只想在方向更改时配置DetailFragment

public class DetailFragment extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            WindowManager wm = getActivity().getWindowManager();

            Display d = wm.getDefaultDisplay();
            if(d.getWidth() > d.getHeight()){
                mDetailView = inflater.inflate(R.layout.detail, container, false);
            }else{
                mDetailView = inflater.inflate(R.layout.detail_port, container, false);
            }
            gridView = (GridView)mDetailView.findViewById(R.id.grid);
        return mDetailView;
        }
}

所以,我必须onConfigurationChangedSubTopicFragment下面重写是SubTopicFragment extend ListFragment

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    updateDetial(topic_index, args.getString("group_id"), args.getString("group_name"));
}

public void updateDetial(int subTopicPosition,String groupID, String groupName){
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    DetailFragment detailView = (DetailFragment)getFragmentManager()
                                .findFragmentById(R.id.detailFrame);
    DetailFragment detailFrag = new DetailFragment(topic_index, subTopicPosition, groupID, groupName);

    if(detailView==null){

        ft.add(R.id.detailFrame, detailFrag);
        ft.commit();
    }else{
        ft.replace(R.id.detailFrame, detailFrag);
        ft.commit();
    }
}

它运作良好。但是当我移动到另一个活动时出现问题,改变方向并按下返回按钮,它会导致强制关闭我该如何解决这个问题?

06-13 10:15:54.758: E/AndroidRuntime(1050): FATAL EXCEPTION: main
06-13 10:15:54.758: E/AndroidRuntime(1050): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1192)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1203)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:557)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.BackStackRecord.commit(BackStackRecord.java:541)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at com.april.android.SubTopicFragment.updateDetial(SubTopicFragment.java:120)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at com.april.android.SubTopicFragment.onConfigurationChanged(SubTopicFragment.java:259)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.FragmentManagerImpl.dispatchConfigurationChanged(FragmentManager.java:1706)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.Activity.onConfigurationChanged(Activity.java:1402)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3298)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:3427)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1113)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.os.Looper.loop(Looper.java:132)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at android.app.ActivityThread.main(ActivityThread.java:4123)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at java.lang.reflect.Method.invokeNative(Native Method)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at java.lang.reflect.Method.invoke(Method.java:491)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-13 10:15:54.758: E/AndroidRuntime(1050):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

几乎所有关于您的代码的内容都是错误的。

        Display d = wm.getDefaultDisplay();
        if(d.getWidth() > d.getHeight()){
            mDetailView = inflater.inflate(R.layout.detail, container, false);
        }else{
            mDetailView = inflater.inflate(R.layout.detail_port, container, false);
        }
        gridView = (GridView)mDetailView.findViewById(R.id.grid);

不要这样做 - 该平台使用命名约定有自己的加载布局的方式。您的代码应如下所示:

    mDetailView = inflater.inflate(R.layout.detail, container, false);
    gridView = (GridView)mDetailView.findViewById(R.id.grid);

如果您希望它在不同方向看起来不同,请在两者中放置两个 detail.xml 文件,layout-port操作系统layout-land将加载正确的文件(并处理对所有资源 ID 进行重复数据删除,这可能是您的问题)。还:

DetailFragment detailView = (DetailFragment)getFragmentManager()
                            .findFragmentById(R.id.detailFrame);
DetailFragment detailFrag = new DetailFragment(topic_index, subTopicPosition, groupID, groupName);

if(detailView==null){

    ft.add(R.id.detailFrame, detailFrag);
    ft.commit();
}else{
    ft.replace(R.id.detailFrame, detailFrag);
    ft.commit();
}

您已经获得了对 Fragment 的访问权限。然后,您创建一个全新的片段。然后,您在添加新片段之前检查您的第一个片段是否为空?.add() 和 .replace() 是不同的,因为 add() 专门不会删除前一个片段 - replace() 不会抛出错误,但是如果没有前一个片段要删除,所以你可以每次都使用它.

    ft.replace(R.id.detailFrame, new DetailFragment(topic_index, subTopicPosition, groupID, groupName);
    ft.commit();
于 2012-06-13T09:49:04.700 回答
0

您不应该在 onCreateView 中加载两个不同的布局 ID

为您的两个布局使用一个布局 ID

你能做的是

布局文件夹中的 R.layout.detail

R.layout.detail_port 重命名为 R.layout.detail 放在 res 的 layout-port 文件夹中

然后 inflater.inflate(R.layout.detail, container, false); 会根据方向自动给出具体的布局

阅读本文档http://developer.android.com/guide/topics/resources/providing-resources.html

于 2012-06-13T05:00:41.070 回答