Error while changing screen orientation using Fragments

Asked

Viewed 109 times

-1

While turning the screen orientation I get this error:

Error android.view.Inflateexception: Binary XML file line #23: Binary XML file line #11: Error inflating class Fragment

Question: What could be causing the error?

Contextualization:

  • The application opens normally but when trying to rotate the screen the error happens, after receiving the error I placed the code of Activity that contains the hostfragment inside a Try-catch, and when rotating the screen the app did not close, but did not inflate the layout. I’ve tried to change many things and various solutions from various sites and friends but none of them could actually solve the problem.
  • After several tests I realized that only Fragment login is doing that if I go to any other the mistake does not happen.

The error points exactly to this line: setContentView(R.layout.activity_login);

This is Activity where Fragment host is:

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.navigation.ui.AppBarConfiguration;

public class LoginActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

}

Fragment host is in Activity xml file:

<fragment
    android:id="@+id/host_fragment_login"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/login_navigation"
    />

Error log:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: athamuso.easyservico.easyparceiro, PID: 3017
    java.lang.RuntimeException: Unable to start activity ComponentInfo{athamuso.easyservico.easyparceiro/athamuso.easyservico.easyparceiro.LoginActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #11: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3131)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5030)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4939)
        at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1986)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:220)
        at android.app.ActivityThread.main(ActivityThread.java:6939)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
     Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #11: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
     Caused by: java.lang.IllegalArgumentException
        at androidx.lifecycle.LifecycleRegistry.downEvent(LifecycleRegistry.java:263)
        at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:314)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:118)
        at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.java:150)
        at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.java:130)
        at androidx.navigation.NavController.popBackStackInternal(NavController.java:325)
        at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:426)
        at androidx.navigation.NavController.onGraphCreated(NavController.java:636)
        at androidx.navigation.NavController.setGraph(NavController.java:586)
        at androidx.navigation.NavController.setGraph(NavController.java:551)
        at androidx.navigation.NavController.setGraph(NavController.java:533)
        at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:238)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:135)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at athamuso.easyservico.easyparceiro.LoginActivity.onCreate(LoginActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:7148)
        at android.app.Activity.performCreate(Activity.java:7139)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3111)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5030)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4939)
        at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1986)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:220)
        at android.app.ActivityThread.main(ActivityThread.java:6939)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
I/Process: Sending signal. PID: 3017 SIG: 9
  • I saw that they gave a -1 in my question, is there something wrong ? I am reading about how to ask the questions in the forum but I found no mistakes, I would appreciate it if someone can help me improve my question. I’ve looked around a lot and couldn’t get anything to help.

1 answer

0

Apparently the problem is in the Android IDE Studio when renaming the ID of the first Fragment that is in Graph(Area responsible for navigation) is automatically renaming the navgation id itself, so the navigation and Fragment were getting identical ID:

    <?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_login"
    app:startDestination="@id/nav_login">

    <fragment
        android:id="@+id/nav_cadastro"
        android:name="Cadastro"
        android:label="Cadastro"
        tools:layout="@layout/cadastro_fragment" />
    <fragment
        android:id="@+id/nav_login"
        android:name="teste"
        android:label="fragment_teste"
        tools:layout="@layout/fragment_teste" >
        <action
            android:id="@+id/action_teste2_to_nav_cadastro"
            app:destination="@id/nav_cadastro" />
    </fragment>

</navigation>

Solution: Do not use the visual editor of the IDE, choose to change it manually at least until you adjust the ID, do it directly by the code if it will not change both ID at the same time and the bug will persist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.