Failed with the Fragment in AndroidStudio Kotlin

0

Good, my mistake is the following. I'm doing an app that when I start to show a screen (MainActivity) with a button, this button when I press it takes me to another screen (MainActivity2). This works all right. Now what I wanted was to work with the fragments, when turning the screen to show both fragment half would show the (MainActivity) and the other half (MainActivity2). I did this by creating a landscape of MainActivity and added the 2 fragments in this landscape. The problem comes when I start the app, I turn the screen, 2 fragments are shown perfectly, I turn the screen again, the start screen is displayed, I turn again and ... pum! Break the app.

These are the errors that it gives me:

02-05 19: 33: 48.396 8983-8983 / com.example.user.kotlin, E / AndroidRuntime: FATAL EXCEPTION: main                                                                                 Process: com.example.pargibaycalvo.kotlin, PID: 8983                                                                                 java.lang.IllegalStateException: Fragment has not been attached yet.                                                                                     at android.support.v4.app.Fragment.instantiateChildFragmentManager (Fragment.java:2308)                                                                                     at android.support.v4.app.Fragment.getChildFragmentManager (Fragment.java:773)                                                                                     at android.support.v4.app.FragmentActivity.markState (FragmentActivity.java:967)                                                                                     at android.support.v4.app.FragmentActivity.onSaveInstanceState (FragmentActivity.java:527)                                                                                     at android.support.v7.app.AppCompatActivity.onSaveInstanceState (AppCompatActivity.java:509)                                                                                     at android.app.Activity.performSaveInstanceState (Activity.java:1442)                                                                                     at android.app.Instrumentation.callActivityOnSaveInstanceState (Instrumentation.java:1301)                                                                                     at android.app.ActivityThread.callCallActivityOnSaveInstanceState (ActivityThread.java:4590)                                                                                     at android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:4541)                                                                                     at android.app.ActivityThread.-wrap19 (ActivityThread.java)                                                                                     at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1510)                                                                                     at android.os.Handler.dispatchMessage (Handler.java:102)                                                                                     at android.os.Looper.loop (Looper.java:154)                                                                                     at android.app.ActivityThread.main (ActivityThread.java:6247)                                                                                     at java.lang.reflect.Method.invoke (Native Method)                                                                                     at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:872)                                                                                     at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)

// in the landscape xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentA"
    android:name="com.example.user.kotlin.MainFragment"
    android:layout_width="0dp"
    android:layout_height="310dp"
    android:layout_marginEnd="2dp"
    app:layout_constraintEnd_toStartOf="@+id/fragmentB"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout="@layout/fragment_blank_a"
    tools:ignore="RtlCompat" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentB"
    android:name="com.example.user.kotlin.MainFragment2"
    android:layout_width="0dp"
    android:layout_height="311dp"
    android:layout_marginEnd="1dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/fragmentA"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints,RtlCompat"
    tools:layout="@layout/fragment_blank_b" />

E searched and report that you need to add the onAttach but still does not work. Do you know what this problem may be?

    
asked by Pedro 05.02.2018 в 19:47
source

0 answers