App crashes when I switch to an activity that contains a fragment (Binary XML file line #10: Binary XML file line #10: Error inflating class fragment)

Viewed 72

My app crashes when I press a button to open another activity that contains a fragment. I am new to Android Studio and I can't seem to find any solution.

In the activity_main.xml I removed the other buttons for this question because I want to focus mainly on this monday_btn.

I researched this type of error and tried many tips but none of them seemed to work.

MainActivity.java

    package com.example.dietmanagement;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*Intent i = getIntent();
        String current_user = i.getStringExtra("current_user");
        TextView current_user_txtview = findViewById(R.id.current_user);
        current_user_txtview.setText("Welcome, " + current_user);*/

        Button monday = findViewById(R.id.monday_btn);
        monday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent day = new Intent(MainActivity.this, DayActivity.class);

                startActivity(day);
            }
        });

    }

}

DayActivity.java

    package com.example.dietmanagement;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;

public class DayActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day);

        FragmentTransaction fragmentTransaction;
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        DayFragment dayFragment = new DayFragment();
        fragmentTransaction.add(R.id.dayfragment, dayFragment);
        fragmentTransaction.commit();


    }
}

DayFragment.java

    package com.example.dietmanagement;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class DayFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_day, container, false);

        return view;
    }
}

fragment_day.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_green"
    tools:context=".DayFragment">
    
    <TextView
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/day" />

</FrameLayout>

activity_day.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_green"
    tools:context=".DayActivity">

    <fragment
        android:id="@+id/dayfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </fragment>


</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_green"
    tools:context=".MainActivity">

<Button
    android:id="@+id/monday_btn"
    android:layout_width="140dp"
    android:layout_height="86dp"
    android:text="@string/monday"
    android:textColor="#6FED84"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.091"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/main_title"
    app:layout_constraintVertical_bias="0.082" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the error message I get:

2021-03-21 23:34:35.459 29479-29479/com.example.dietmanagement E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dietmanagement, PID: 29479
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dietmanagement/com.example.dietmanagement.DayActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
 Caused by: java.lang.NullPointerException
    at java.lang.VMClassLoader.findLoadedClass(Native Method)
    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.Fragment.instantiate(Fragment.java:617)
    at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3602)
    at android.app.FragmentController.onCreateView(FragmentController.java:98)
    at android.app.Activity.onCreateView(Activity.java:6187)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
    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.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:696)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
    at com.example.dietmanagement.DayActivity.onCreate(DayActivity.java:16)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2 Answers

Your <fragment> element does not say what fragment it is supposed to be loading. You need to add an android:name to <fragment>, where the value of android:name is the fully-qualified class name of the fragment:

<fragment
    android:id="@+id/dayfragment"
    android:name="com.example.dietmanagement.DayActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</fragment>

You're attempting to load a fragment in two different ways - directly via the layout with the <fragment> tag, which requires you to specify which Fragment you actually want to load, and as a transaction in the activity which requires a layout in which to put the fragment.

// This says - "hey Android load up a fragment for me", but you don't say which
<fragment
    android:id="@+id/dayfragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</fragment>

But ...

// This says - "hey Android, shove the new "dayFragment" into the view R.id.dayFragment".
// But based on your previous XML declaration, R.id.dayFragment is itself already a
// Fragment!
FragmentTransaction fragmentTransaction;
fragmentTransaction = getSupportFragmentManager().beginTransaction();
DayFragment dayFragment = new DayFragment();
fragmentTransaction.add(R.id.dayfragment, dayFragment);
fragmentTransaction.commit();

The easiest solution for you, based on what you've posted is to replace the <fragment> element with a FragmentContainerView and either leave the code in your activity or declare the class in the xml. Please see the documentation for examples on how to do this in both ways.

Related