0

According to answers to an existing question there used to be problems with using fragments inside fragments in android but there is supposed to be now a way for Android to be able to handle them.

While the existing question deals with creating the fragments in Java, I want to create them directly in Xml via:

<fragment
    android:layout_width="100dp"
    android:layout_height="100dp"

    class="com.ra.ra_e_akte.fragments.MyFragment"

    android:id="@+id/myFragment"
    />

Unfortunately, I get the error:

2019-07-29 15:37:06.586 16529-16529/com.ra.ra_e_akte E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ra.ra_e_akte, PID: 16529
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ra.ra_e_akte/com.ra.ra_e_akte.activities.DefaultStacksActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #66: Binary XML file line #66: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2962)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1701)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6949)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #66: Binary XML file line #66: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #66: Binary XML file line #66: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #66: Error inflating class fragment
     Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.ra.ra_e_akte.fragments.SyncButton: calling Fragment constructor caused an exception
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:532)

onCreate in the parent fragment:

override fun onCreateView(inflater: LayoutInflater,
                          container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.header_fragment, container, false)

    return view
}

Is there a functioning way to declare fragments inside xml of other fragments?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • android:name="com.ra.ra_e_akte.fragments.MyFragment" try this. – Anmol Jul 29 '19 at 13:45
  • Share code of `MyFragment ` as if you are not extending MyFragment by android's `Fragment` class then the same error will occur. Just for info `class` and `android:name` both are working for me. – Anmol Jul 29 '19 at 13:56

1 Answers1

2
<fragment android:name="com.ra.ra_e_akte.fragments.MyFragment"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/myFragment"
    />

try this code

  • I just tried class is also working fine in my demo project. – Anmol Jul 29 '19 at 13:56
  • Fragment life cycle in onCreate method might be called during the onCreate of the Activity life cycle. But this not the case all the times. Therefore, just to be on safer side if you wish to access the views in fragment do it inside onActivityCreated() method. This gives you the confirmation that the activity has been created and View hierarchy has been loaded into the memory. :) – PRASHANT KARELIYA Jul 29 '19 at 14:04
  • how does xml layout tag `android:name` give's confirmation? this tag is only to give android specific class file's to xml layout. I think you are confusing lifecycle with xml tag's somehow.The error is because layout is not inflated properly. – Anmol Jul 29 '19 at 14:08