【问题标题】:Orientation Change: IllegalStateException when DialogFragment is visible方向更改:DialogFragment 可见时出现 IllegalStateException
【发布时间】:2013-02-08 22:32:33
【问题描述】:

我想让 Android 处理方向更改。 不幸的是,每次打开对话框并发生方向更改时它都会崩溃。 我已经怀疑原因但不知道如何处理:

Application 首次启动时的 backstack 如下所示:

02-07 15:25:36.517: I/System.out(14541): Added Fragments:
02-07 15:25:36.517: I/System.out(14541):   #0: PortraitModeFragment{41963330 #0 id=0x7f090001 ListViewFragment}

太好了,现在我们打开一个对话框:

02-07 15:26:03.316: I/System.out(14541): Added Fragments:
02-07 15:26:03.316: I/System.out(14541):   #0: PortraitModeFragment{41963330 #0 id=0x7f090001 ListViewFragment}
02-07 15:26:03.326: I/System.out(14541):   #1: MyDialogFragment{4199f6a0 #1 MyDialogFragment}

现在旋转设备时:

02-07 15:26:37.502: E/AndroidRuntime(14541): FATAL EXCEPTION: main
02-07 15:26:37.502: E/AndroidRuntime(14541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.os.Looper.loop(Looper.java:137)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at java.lang.reflect.Method.invoke(Method.java:511)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at dalvik.system.NativeStart.main(Native Method)
02-07 15:26:37.502: E/AndroidRuntime(14541): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.Activity.setContentView(Activity.java:1881)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at com.example.testapp.MainActivity.onCreate(MainActivity.java:17)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.Activity.performCreate(Activity.java:5104)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-07 15:26:37.502: E/AndroidRuntime(14541):    ... 12 more
02-07 15:26:37.502: E/AndroidRuntime(14541): Caused by: java.lang.IllegalStateException: Fragment com.example.testapp.fragments.LandscapeModeFragment did not create a view.
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:303)
02-07 15:26:37.502: E/AndroidRuntime(14541):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

有趣的事实:如果我首先转动设备并随后打开对话框并改变方向,则不会发生这种情况,因为两个片段都已预先加载并且和平地存在于后台堆栈中:

02-07 15:34:00.015: I/System.out(15412):   #0: PortraitModeragment{419b99b0 #0 id=0x7f090001 ListViewFragment}
02-07 15:34:00.015: I/System.out(15412):   #1: LandscapeModeFragment{419b9b88 #1 id=0x7f090003 LandscapeModeFragment}
02-07 15:34:00.025: I/System.out(15412):   #2: MyDialogFragment{419d1da0 #2 MyDialogFragment}

我无法让用户在使用我的应用程序之前旋转智能手机,那该怎么办? ;)

重现:

如何将 Dialog 添加到 backstack:

MyDialogFragment myTvShowDialog = new MyDialogFragment();
myTvShowDialog.show(getActivity().getSupportFragmentManager(),myTvShowDialog.TAG);

我所做的是为 MainActivity 创建了两个 xml 文件:

1 . res/layout/main.xml(纵向模式):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">
     <TextView android:id="@+id/textView1" android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="layout-land/PORTRAIT" />
     <fragment class="com.example.testapp.fragments.PortraitModeFragment"
                        android:id="@+id/fragment" android:layout_weight="1"
                        android:layout_width="0px" android:layout_height="match_parent" android:tag="PortraitModeFragment">
     </fragment>
</LinearLayout>

2 。 res/layout-land/main.xml (横向模式)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" android:orientation="vertical">
    <TextView   android:id="@+id/textView1"   android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="layout-land/LANDSCAPE" />
    <fragment class="com.example.testapp.fragments.LandscapeModeFragment"
            android:id="@+id/LandscapeModeFragment" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="0dp" android:tag="LandscapeModeFragment">
     </fragment>
</LinearLayout>

MainActivity.java:

public class MainActivity extends FragmentActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate()");
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().dump("", null,
                new PrintWriter(System.out, true), null);
    }
}

FragmentPortraitMode:

public class PortraitModeFragment extends Fragment {
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragment = inflater.inflate(R.layout.portraitmodefragment_layout,
            null);
    Button findViewById = (Button) fragment.findViewById(R.id.button1);
    findViewById.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            MyDialogFragment myDialog = new MyDialogFragment();
            myDialog.show(getActivity().getSupportFragmentManager(),
                    MyDialogFragment.TAG);
        }
    });
    TextView textView = (TextView) fragment.findViewById(R.id.textView1);
    textView.setText(TAG);
    return fragment;
}

片段横向模式:

public class LandscapeModeFragment extends Fragment {

    public static final String TAG = "LandscapeModeFragment";

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragment = inflater.inflate(R.layout.landscapemode_fragment_layout,
            container, false);
    return fragment;
}
}

landscapemode_fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
            <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LANDSCAPEFRAGMENT" />
</LinearLayout>

portraitmode_fragment_layout_xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button   android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

【问题讨论】:

  • 在 LandscapeModeFragment 中无法充气的“generic_dialog_layout.xml”是什么/在哪里?
  • @Kerry 添加了对话框的布局,我承认这里的命名很弱。它只是一个通用布局...
  • 我将使用的 layout.xml 的命名更改为 Landscape_fragment_layout.xml 现在命名应该更清晰。

标签: android android-orientation android-dialogfragment


【解决方案1】:

好吧,我找到了一个同样有效的解决方法。

第一:问题是什么?

在 layout-xml 中定义的 Fragment 和每个 FragmentManager 添加的 Fragment 具有不同的生命周期 abd,尽管可能会因 FragmentManager 的意外行为(异常)导致应用程序崩溃而与 layout-fragments 混淆。

As Dianne Hackborn mentioned in the Google Groups Forum

在 XML 中定义片段主要是针对正在发生的事情 留下来。如果要添加和删除,您应该 可能始终动态地进行。

我可以在未来的版本中对此进行研究,但请注意 无论我做什么,你都可以很容易地让自己陷入糟糕的境地 this -- 例如,如果您没有唯一的包含视图组 对于片段,当您删除/添加片段时会发生什么 层次结构中的点将表现不一致。

所以我们不允许混合世界(FragmentManager-Fragments 和 LayoutXML-Fragments)。不幸的是,这也涉及在运行时添加的 DialogFragments。会发生什么:Android-Framework 将orientation-change 新布局创建的Fragment 添加到backstack 中。 FragmentManager 尝试将 DialogFragment 添加到与新创建的 Layout-Fragment 相同的索引上。那就是事情变得混乱并发生 IllegalStateException 的时候。

那么,解决方法是什么?

大多数将 Fragment 用于不同方向的示例都涉及将 FrameLayout 定义为容器,以将正确的 Fragment 放入其中。所以我们将一直使用 FragmentManager 并从我们的设计中禁止 LayoutFragments:

ActivityMain 仅使用这个简单的 xml 作为它的 contentView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container_id"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
</FrameLayout>

当它启动时,它会向容器中注入正确的 Fragment

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity_framelayout);
    createFragmentIfNotExits();
}
private void createFragmentIfNotExits() {
    int orientation = getResources().getConfiguration().orientation;
    Fragment newFragment;
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        newFragment = new TvShowListPortraitModeFragment();
    } else {
        newFragment = new TvShowListLandscapeModeFragment();
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container_id, newFragment);
    ft.commit();
}

缺点: 不幸的是,以这种方式无法保留片段的已保存状态。或者至少我还需要弄清楚我们如何做到这一点。

我说了这么多。我不会将此答案标记为“已回答”,因为我并没有真正使它起作用,也许有人会弄清楚并发布一个更简单的解决方案。当然,我会将此标记为正确的。

【讨论】:

  • 因为 android 开发团队的男生女生不费心去修复 bug,而是引入带有新 bug 的新版本,我认为你描述的官方 bug 不会有更好的解决方案 @987654322 @
  • 我发现 code.google.com/p/android/issues/detail?id=18529#c8 的错误中描述的替代解决方法很有用
猜你喜欢
  • 2023-03-18
  • 2019-06-02
  • 2012-10-01
  • 1970-01-01
  • 2016-08-28
  • 2015-06-09
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
相关资源
最近更新 更多