获取“片段未创建视图”。在添加其他没有UI的片段之后 [英] Getting "Fragment did not create a view" after addition of other Fragment without UI

查看:91
本文介绍了获取“片段未创建视图”。在添加其他没有UI的片段之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很奇怪的问题。我在应用程序的几乎所有活动中都添加了一个通用片段。该片段显示了播放器栏的一个小版本。因此它会收听一些广播以更新当前音乐的名称,并具有一些控制功能,例如播放/暂停。

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.

就像我说的那样,我几乎在我的每个活动中都添加了此片段应用程序,我从来没有任何问题。但是现在,我需要创建一个没有UI并保留下来的新Fragment(setRetainInstance(true))。添加此新片段后,一切似乎都还不错。

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 addition 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

此异常发生在Activity中已经存在的Fragment中,而不是新的分段。如果我使用播放器栏的小版本删除了片段,并保留了没有UI的新片段,则一切正常。如果我只保留旧片段并删除新片段,它也可以工作。

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();
}

更新:

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

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.

现在,Activity的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.

这篇关于获取“片段未创建视图”。在添加其他没有UI的片段之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆