我有一个非常奇怪的问题.我的应用程序的几乎每个活动都添加了一个常见的片段.该片段显示播放器栏的小版本.因此,它会收听一些广播来更新当前音乐的名称,并具有一些控制,如播放/暂停.
就像我说的,我将这个片段添加到我应用程序的几乎每个活动中,我从来没有遇到任何问题.但现在,我需要创建一个没有UI的新片段(被保留)(setRetainInstance(true)).在这个新片段的欢迎之后,一切似乎都可以.直到我旋转设备并且活动崩溃.
所以,查看日志,我看到以下异常:
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中,而不是发生在新的Fragment中.如果我使用小版本的播放器栏删除片段,并保留没有UI的新片段,一切正常.如果我只保留旧片段并删除新的片段,它也可以工作.
更多细节:
旧片段通过布局XML添加.以下是活动的XML:
<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>
新片段(wih no UI)以下列方式添加:
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),以查看问题是否与此相关.但问题仍在发生.
解决方法
哦,我的上帝,我不能理解它.我解决了我的问题,只是设置有问题的片段的ID.
现在,XML的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:id="@+id/player_reduzido_fragment" android:layout_width="fill_parent" android:layout_height="wrap_content" android:name="com.soongz.ui.fragment.PlayerReduzidoFragment"/> </LinearLayout>
我不知道为什么它必须是一个错误.