获得"片段并没有创建一个视图"其他片段ADITION无UI后,片段、并没有、视图、创建一个

2023-09-06 03:24:10 作者:他却无意再说

我有一个很奇怪的问题。我有一个在我的应用程序,几乎每一个活动增加了一个共同的片段。该片段展示了一个小型版本的播放器吧。所以,听一些广播来更新当前音乐的名称和具有一定的控制,如播放/暂停。

I'm having a really weird problem. I have a common fragment that is added in almost every activity of my app. This fragment shows a small version of the player bar. So it listens some broadcasts to update the current music's name and has some controls, like play/pause.

就像我说的,我添加此片段在我的应用程序的几乎每一个活动,我从来没有任何问题的。但现在,我需要建立一个新的片段,有没有用户界面和保留(setRetainInstance(真))。这个新片段的ADITION后,一切似乎是确定。直到我转动设备和活动崩溃了。

Like I said, I add this fragment in almost every activity of my app and I've never had any problem with it. But now, I needed to create a new Fragment that has no UI and that is retained (setRetainInstance(true)). After the adition of this new Fragment, everything seemed to be ok. Until I rotated the device and the activity crashed.

所以,看在日志中,我看到了以下异常:

So, looking in the log, I see the following exception:

07-05 14:10:23.818: ERROR/AndroidRuntime(25922): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soongz/com.soongz.ui.PlaylistActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
        at android.app.ActivityThread.access$800(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
        at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
        at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
        at net.simonvt.menudrawer.MenuDrawer.setContentView(MenuDrawer.java:964)
        at com.soongz.ui.BaseComMenuActivity.setContentViewComMenu(BaseComMenuActivity.java:31)
        at com.soongz.ui.PlaylistActivity.createView(PlaylistActivity.java:111)
        at br.com.cybereagle.androidlibrary.ui.EagleActivity.onCreate(EagleActivity.java:57)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
        ... 12 more
        Caused by: java.lang.IllegalStateException: Fragment com.soongz.ui.fragment.PlayerReduzidoFragment did not create a view.
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:303)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
        ... 26 more

这异常发生,而不是发生在新的片段,这是已经在活动的片段。如果我删除的片段,小版本的播放器吧,保持新的片段没有用户界面,一切工作正常。它也可以,如果我只保留了旧片段,并删除新的。

This exception is happening in the Fragment that was already in the Activity, instead of happening in the new Fragment. If I remove the fragment with the small version of the player bar and keep the new Fragment without UI, everything works normally. It also works if I keep just the old fragment and remove the new one.

详细信息: 旧片段通过布局XML增加。下面是活动的XML:

More details: The old fragment is added via layout XML. Here is the XML of the Activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <fragment
        android:name="com.soongz.ui.fragment.ListaDeMusicasFragment"
        android:id="@+id/lista_de_musicas_fragment"
        style="?layoutListViewMusicas" />

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:name="com.soongz.ui.fragment.PlayerReduzidoFragment"/>
</LinearLayout>

新片段(王氏没有UI)通过以下方式添加:

The new fragment (wih no UI) is added in the following way:

FragmentManager fragmentManager = getSupportFragmentManager();
operacoesEmBackgroundFragment = (OperacoesEmBackgroundFragment) fragmentManager.findFragmentByTag(TAG_OPERACOES_EM_BACKGROUND_FRAGMENT);

if(operacoesEmBackgroundFragment == null){
    operacoesEmBackgroundFragment = new OperacoesEmBackgroundFragment();
    fragmentManager.beginTransaction()
            .add(operacoesEmBackgroundFragment, TAG_OPERACOES_EM_BACKGROUND_FRAGMENT)
            .commit();
}

在此先感谢的人谁可以帮我。

Thanks in advance for anyone who can help me.

更新:

我删除了从没有UI的片段setRetainInstance(真),看看问题是否与此有关。但问题仍然存在。

I removed the setRetainInstance(true) from the fragment with no UI to see if the problem is related to this. But the problem is still occurring.

推荐答案

哦,天哪,我可不敢相信它。我解决我的问题只是设置这是有问题的片段的ID。

Oh my God, I can't beleive it. I solved my problem just setting the ID of the fragment that was having problem.

现在,该活动的XML的布局是这样的:

Now, the XML's layout of the Activity is like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <fragment
        android:name="com.soongz.ui.fragment.ListaDeMusicasFragment"
        android:id="@+id/lista_de_musicas_fragment"
        style="?layoutListViewMusicas" />

    <fragment
        android:id="@+id/player_reduzido_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:name="com.soongz.ui.fragment.PlayerReduzidoFragment"/>
</LinearLayout>

我不知道为什么。它必须是一个错误。

I don't know why. It must be a bug.