我有一个非常奇怪的问题。我有一个常见的片段,几乎在我的应用程序的每个活动中都添加了。此片段显示了一个小版本的播放器栏。所以它会收听一些广播以更新当前音乐的名称,并有一些控制,如播放/暂停。
就像我说的,我几乎在我的应用程序的每个活动中都添加了这个片段,我从来没有遇到任何问题。但现在,我需要创建一个没有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>
以下列方式添加新片段(无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),以查看问题是否与此相关。但问题仍然存在。
答案 0 :(得分:17)
天哪,我无法相信。我解决了我的问题,只是设置了有问题的片段的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>
我不知道为什么。它一定是个bug。
答案 1 :(得分:1)
为了将来参考,我发现我的问题在于我的片段onCreateView()
方法。我有以下内容:
private lateinit var containingView : View
// ......
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
containingView = inflater.inflate(R.layout.my_fragment_layout, container, false)
return view
}
.....导致错误,因为当我将布局膨胀到containingView
时,我实际上正在返回view
(恰好是正确的片段类的属性)类型)。更正后的代码是这样的:
private lateinit var containingView : View
// ......
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
containingView = inflater.inflate(R.layout.my_fragment_layout, container, false)
return containingView
}