Home > Back-end >  Why do I get the android.view.InflateException when using FragmentContainerView
Why do I get the android.view.InflateException when using FragmentContainerView

Time:09-27

I am still pretty new to fragments and I am sure I do not understand things correctly. But I hope I can learn from this now.

I follow this guide for connecting my xml with the fragments: https://developer.android.com/guide/fragments/create. But when starting the app I get an error that says that some parts are not compatible.

Here are my files:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    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"
    tools:context=".MainActivity"
    android:id="@ id/main"
    android:orientation="horizontal"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="30dp">


    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/brand"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/item"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold"
        />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/gibson_1"
        />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/price"
            android:gravity="right"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="right"
            >
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:layout_marginRight="15dp"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add to cart"
                android:drawableLeft="@drawable/drawablestart"
                android:id="@ id/addCart"
                />
        </LinearLayout>



    </FrameLayout>

</androidx.fragment.app.FragmentContainerView>

MainActive.java

package at.hlpinkafeld.android.musicstore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public MainActivity() {
        super(R.layout.activity_main);
    }

    @Nullable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .setReorderingAllowed(true)
                    .add(R.id.main, ProductDetailFragment.class, null)
                    .commit();
        }


    }
}

fragment.java

package at.hlpinkafeld.android.musicstore;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.textview.MaterialTextView;

public class ProductDetailFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.activity_main,container,false);
        System.out.println(view);
        return view;
    }
}

the error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: at.hlpinkafeld.android.musicstore, PID: 13020
    java.lang.RuntimeException: Unable to start activity ComponentInfo{at.hlpinkafeld.android.musicstore/at.hlpinkafeld.android.musicstore.MainActivity}: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
     Caused by: java.lang.IllegalStateException: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
        at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.kt:266)
        at android.view.ViewGroup.addView(ViewGroup.java:5048)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1131)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:710)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:356)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
        at at.hlpinkafeld.android.musicstore.MainActivity.onCreate(MainActivity.java:25)
        at android.app.Activity.performCreate(Activity.java:8051)
        at android.app.Activity.performCreate(Activity.java:8031)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

For help and an explanation why it didn't work like that, would be nice

CodePudding user response:

I think you might want to try loading a simple fragment into an activity.

Try this code. It's working for you.

For more info: click

Note: Always create a new object like this to load fragments new ProductDetailFragment() , and use setContentView(); inside of onCreate().

1. First of all, create the perfect activity to load a fragment.

MainActivity.class

public class MainActivity extends AppCompatActivity {

String TAG = "MainActivity";

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

    getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.main, new ProductDetailFragment(), null)
                .commit();
    }
}

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   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"
    tools:context=".MainActivity"
    android:id="@ id/main"
    android:orientation="horizontal"
    >
</RelativeLayout>

2. create fragment ProductDetailFragment.class

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ProductDetailFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_product_details,container,false);
        System.out.println(view);
        return view;
    }
}

3. make diffrent layout for fragment fragment_product_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/brand" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/item"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/gibson_1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/price" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="horizontal">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:inputType="phone" />

        <Button
            android:id="@ id/addCart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/drawablestart"
            android:text="Add to cart" />
    </LinearLayout>
</FrameLayout>

CodePudding user response:

You misunderstood the actual code in the link :- https://developer.android.com/guide/fragments/create

You put the fragment xml code inside your fragment container view which is inside your main activity.

You just need to declare the fragment container view defined below.

The fragment container view is main container which contains your fragment like below in your main activity.

<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="<provide your fragment class here from your code it is ProductDetailFragment>" />

Then create the fragment xml file in layout directory named fragment_product_detail:-

Then put your fragment xml code in it. like below.

fragment_product_detail.xml :-

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp">


<TextView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="@string/brand"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/item"
    android:textAppearance="@style/TextAppearance.AppCompat.Headline"
    android:textStyle="bold"
    />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    app:srcCompat="@drawable/gibson_1"
    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/price"
        android:gravity="right"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="right"
        >
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:layout_marginRight="15dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add to cart"
            android:drawableLeft="@drawable/drawablestart"
            android:id="@ id/addCart"
            />
    </LinearLayout>



</FrameLayout>

Then in your fragment onCreateView method make sure to inflate the fragment layout like below :-

View view =inflater.inflate(R.layout.fragment_product_detail,container,false);

This will resolve your problem. Let me know if you still face any issue.

  • Related