javaandroidandroid-fragmentsinflate-exception

InflateException, Fragment$InstantiationException for Fragments in Android Studio


I have created two demo Fragment classes and corresponding fragment layout using Right Click on Java project folder -> New -> Fragment and kept everything generated by IDE as it is.

My files are -

In my activity_main.xml file I have used the fragments as below -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:layout_margin="20dp">


    <fragment
        android:id="@+id/fragment"
        android:name="com.example.fragmentdemo.TopSectionFragmnet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_top_section" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentdemo.BottomImageFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_bottom_image"
        android:layout_below="@id/fragment"/>
    
</RelativeLayout>

The MainActivity.java file is the default generated by IDE

When I am running the program in emulator, my IDE says 'success' but emulator says 'app keeps stopping -> app information, close app'

The Analyze -> Stack Trace or Thread Dump info is as below -


01/14 02:47:32: Launching 'app' on Pixel 4 API 28.
App restart successful without requiring a re-install.
$ adb shell am start -n "com.example.fragmentdemo/com.example.fragmentdemo.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 10882 on device 'Pixel_4_API_28 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fragmentdemo, PID: 10882
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentdemo/com.example.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)
     Caused by: android.view.InflateException: Binary XML file line #11: 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: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.fragmentdemo.TopSectionFragmnet that is not a Fragment
        at android.app.Fragment.instantiate(Fragment.java:526)
        at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547)
        at android.app.FragmentController.onCreateView(FragmentController.java:102)
        at android.app.Activity.onCreateView(Activity.java:6323)
        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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420)
        at android.app.Activity.setContentView(Activity.java:2771)
        at com.example.fragmentdemo.MainActivity.onCreate(MainActivity.java:11)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)
     Caused by: java.lang.ClassCastException
        at android.app.Fragment.instantiate(Fragment.java:526) 
        at android.app.FragmentContainer.instantiate(FragmentContainer.java:53) 
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547) 
        at android.app.FragmentController.onCreateView(FragmentController.java:102) 
        at android.app.Activity.onCreateView(Activity.java:6323) 
        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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420) 
        at android.app.Activity.setContentView(Activity.java:2771) 
        at com.example.fragmentdemo.MainActivity.onCreate(MainActivity.java:11) 
        at android.app.Activity.performCreate(Activity.java:7136) 
        at android.app.Activity.performCreate(Activity.java:7127) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        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:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        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:858) 

I am using Android Studio 4.1.1

How can I properly run the app in emulator?


Solution

  • In fragment_top_section.xml you have to provide the id android:id="@+id/top_fragment"

    Similarly, in fragment_bottom_image.xml you have to provide the id android:id="@+id/bottom_fragment"

    Then, in MainActivity.java you have to add the following import statement -

    import androidx.fragment.app.FragmentManager;
    

    and under the onCreate(Bundle savedInstanceState) you have to add the following code -

            final FragmentManager fragmentManager = getSupportFragmentManager();
            final TopSectionFragment topSectionFragment = new TopSectionFragment();
            fragmentManager.beginTransaction().replace(R.id.top_fragment, topSectionFragment);
            final BottomImageFragment bottomImageFragment = new BottomImageFragment();
            fragmentManager.beginTransaction().replace(R.id.bottom_fragment, bottomImageFragment);
    

    Note: Your MainActivity must extend AppCompatActivity and not Activity