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.
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
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.
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>
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();
}
UPDATE:
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.
Oh my God, I can't beleive it. I solved my problem just setting the ID of the fragment that was having problem.
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.
For future reference, I found that my issue lay in my fragment's onCreateView()
method. I had the following:
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
}
.....which caused an error because while I was inflating the layout to containingView
, I was actually returning view
(which just happens to be a property of the fragment class of the correct type). The corrected code is this:
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
}
来源:https://stackoverflow.com/questions/17494429/getting-fragment-did-not-create-a-view-after-addition-of-other-fragment-withou