Error inflating class fragment android studio

I get the Error Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #11: Error inflating cl...

I get the Error

Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

when I switch via the portrait and the landscape mode. I’m using fragments. My xml is:

 <LinearLayout android:id="@+id/mainLayout"
               android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" >

    <ListView android:id="@+id/android:list"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/> 

    <fragment android:id="@+id/fragmentDetails"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              class="de.androidbuch.activiti.task.TaskDetailsFragment"/> 
</LinearLayout>

If I switch via landscape and portrait mode everything works fine. But when I click on my fragment (and I can see my fragment) and then switch to the other mode I get the error. Any idea how I can solve it? Found some answers here but none of these helped me out…

06-21 14:55:05.600: ERROR/AndroidRuntime(7636): FATAL EXCEPTION: main
06-21 14:55:05.600: ERROR/AndroidRuntime(7636): java.lang.RuntimeException: Unable to start activity         
ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}:   android.view.InflateException: Binary XML file line #11: Error inflating class fragment
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3097)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.access$1600(ActivityThread.java:123)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:997)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.os.Looper.loop(Looper.java:126)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.main(ActivityThread.java:3998)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at java.lang.reflect.Method.invoke(Method.java:491)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at dalvik.system.NativeStart.main(Native Method)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.Activity.setContentView(Activity.java:1771)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at de.androidbuch.activiti.task.TaskActivity.onCreate(TaskActivity.java:83)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     ... 12 more

Dinithe Pieris's user avatar

asked Jun 21, 2011 at 11:58

tsync's user avatar

11

As hdemirchian said, make sure to use:

import android.support.v4.app.Fragment;

And also make sure that the Activity that is using the fragment(s) extends FragmentActivity instead of the regular Activity,

import android.support.v4.app.FragmentActivity;

to get the FragmentActivity class.

Paul's user avatar

Paul

6707 silver badges19 bronze badges

answered Feb 6, 2012 at 22:21

Dandre Allison's user avatar

Dandre AllisonDandre Allison

5,9455 gold badges41 silver badges56 bronze badges

4

The exception android.view.InflateException: Binary XML file line: #... Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can’t rely on that.

For instance the next pattern is wrong:

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

    Button button = getActivity().findViewById(R.id...);
    button.setOnClickListener(...); - another problem: button is null

    return view;
}

Correct pattern #1

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

    Button button = view.findViewById(R.id...);
    button.setOnClickListener(...);

    return view;
}

Correct pattern #2

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button button = getActivity().findViewById(R.id...);
    button.setOnClickListener(...);
}

answered Feb 28, 2013 at 13:51

Grigori A.'s user avatar

Grigori A.Grigori A.

2,5881 gold badge20 silver badges19 bronze badges

8

Make sure your Activity extends FragmentActivity.

answered May 22, 2012 at 21:04

james's user avatar

jamesjames

26k19 gold badges98 silver badges112 bronze badges

3

I had the same error. I was digging all day long, don’t know but I think I tried ~25 solutions on this problem. None worked until at 2AM I found out that I was missing this line at apps manifest xml:

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

That line was inside <application> tag. I really hope this helps for someone.
GL.

answered Jan 25, 2014 at 0:12

Matiss's user avatar

MatissMatiss

5,0092 gold badges26 silver badges27 bronze badges

3

I had the same problem. The solution for me was the order of super.onCreate and setContentView within the FragmentActivity

Following order works fine:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fc_activity_list_profiles);

answered Aug 2, 2011 at 17:42

Wilko's user avatar

WilkoWilko

2401 silver badge4 bronze badges

3

Fragments cannot be nested in XML

Learnt this the hard way — if you nest an XML layout based <fragment> tag inside a (potentially) dynamically loaded fragment from FragmentManager, then you start to get weird errors, trying to inflate your fragment xml.

Turns out, that this is not supported — it will work fine if you do this through purely the FragmentManager approach.

I was getting this problem because I was trying load a fragment inside a <DrawerLayout> from xml, and this was causing a crash in the onCreateView() method when I popped the back stack.

answered Feb 24, 2016 at 18:13

Dhiraj Gupta's user avatar

Dhiraj GuptaDhiraj Gupta

9,2457 gold badges49 silver badges52 bronze badges

5

I was having the same problem as you are facing.
None of the tips on top helped me.
Later, I found that all I had to do is fix my imports from:

import android.app.Fragment;

to:

import android.support.v4.app.Fragment;

answered Jan 31, 2012 at 17:01

hdemirchian's user avatar

2

Have you tried:

 <fragment
            android:name="de.androidbuch.activiti.task.TaskDetailsFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

answered Jun 21, 2011 at 13:08

Blundell's user avatar

BlundellBlundell

74.5k30 gold badges208 silver badges232 bronze badges

1

Make sure there is no exception raised in the onCreateView method of the fragment. If any exception is raised in this method, logcat won’t show exact details of the exception, instead it always shows the message:

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.fragment" 
on path: DexPathList[[zip file "/data/app/com.package/base.apk"],
nativeLibraryDirectories=[/data/app/com.package/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

bhass1's user avatar

bhass1

3302 silver badges7 bronze badges

answered Oct 10, 2012 at 9:32

Lei's user avatar

LeiLei

6501 gold badge6 silver badges13 bronze badges

1

If your TaskDetailsFragment extending android.app.Fragment, do change in onCreateView().

Return your view which you want to show in the Fragment or convert your Layout to view by using LayoutInflater and return it.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mainview = inflater.inflate(R.layout.main, null);
    return mainview;
}

Hope this works. :)

Sergey Glotov's user avatar

answered Dec 28, 2011 at 7:07

Suryavel TR's user avatar

Suryavel TRSuryavel TR

3,4161 gold badge20 silver badges25 bronze badges

Check you class path, this could be the view inflator could not find your class definition as defined in your xml
class=»de.androidbuch.activiti.task.TaskDetailsFragment»
the above path could be wrong.

answered May 7, 2012 at 19:03

mask's user avatar

maskmask

6,0843 gold badges22 silver badges23 bronze badges

0

I was receiving this error for different reasons.

Steps to reproduce:

~> My issue was that I created a brand new blank application.

~> I then generated a custom fragment from the File ~> New File Menu.

~> Proceeded to customize the fragment by adding layouts and buttons etc.

~> Referenced the new custom fragment in the auto generated activity_my.xml that was generated for me when creating the application. Doing this allowed the XML to generate the objects for me.

Heres is the catch when generating the custom fragment via File ~> New File Menu it auto generates an interface function stub and places it at the bottom of the fragment class file.

This means that your MyActivity class must implement this interface. If it does not then the the above error occurs only when referencing the fragment from xml. By removing the reference for the Fragment in the XML completely, and creating the fragment through code in the MyActivity.java class file Logcat generates a more concise error explaining the issue in detail and complaining about the interface. This is demonstrated in the Project Template Activity+Fragment. Although, <~that Project Template does not generate the interface stub.

approxiblue's user avatar

approxiblue

6,89216 gold badges50 silver badges59 bronze badges

answered Aug 24, 2014 at 15:13

xMythicx's user avatar

xMythicxxMythicx

8271 gold badge11 silver badges27 bronze badges

0

For me it was after scrolling in the stack trace i found im missing the permission for android.permission.ACCESS_NETWORK_STATE

after adding this permission it was fixed

answered Aug 3, 2014 at 10:39

Gil's user avatar

GilGil

5685 silver badges9 bronze badges

1

If you have separate layout files for portrait and landscape modes and are getting an inflation error whenever you change orientation after clicking an item, there is most likely a discrepancy between your layout files.

When you get the error, is it only when you click the item in landscape mode or only in portrait mode or both? Does your TaskDetailsFragment activity use a layout file that could have discrepancies between landscape and portrait modes?

answered Jun 21, 2011 at 13:19

Nathan Fig's user avatar

Nathan FigNathan Fig

14.8k9 gold badges42 silver badges57 bronze badges

3

If you are adding Fragment statically that is, in xml, then you might have missed to implement OnFragmentInteractionListener in your Activity class. Then the interface implementation would solve the problem. If you are adding Fragment dynamically, that is, in java class then this is not the solution. Because IDE itself will not allow you to proceed without implementing required interfaces.

answered Aug 25, 2016 at 10:42

Shaheer Palollathil's user avatar

Use androidx.fragment.app.FragmentContainerView instead of fragment. Here is a example:

 <androidx.fragment.app.FragmentContainerView 
          android:id="@+id/fragmentDetails"
          android:layout_height="fill_parent"
          android:layout_width="fill_parent"
          class="de.androidbuch.activiti.task.TaskDetailsFragment"/> 

answered Feb 3, 2022 at 9:47

Fakhriddin Abdullaev's user avatar

1

My problem in this case was a simple instance of having a dumb null pointer exception in one of my methods that was being invoked later in the lifecycle. This was causing the «Error inflating class fragment» exception for me. In short, please remember to check the further down the exception stack trace for a possible cause.

Once I resolved the null pointer exception, my fragment loaded fine.

answered Apr 25, 2014 at 19:21

oddmeter's user avatar

oddmeteroddmeter

7547 silver badges16 bronze badges

None of the solutions mentioned above helped me. In the log I could find the detail of the exception as mentioned below:

06-19 16:20:37.885: E/AndroidRuntime(23973): Caused by: java.lang.RuntimeException: API key not found.  Check that /meta-data/ android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/ is in the application element of AndroidManifest.xml.

I did this and my code was working!

<meta-data  android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCl1yGPZ3mpxxxxxxxAz2R-t7zcWVzrHUL9k"/>
</application>

approxiblue's user avatar

approxiblue

6,89216 gold badges50 silver badges59 bronze badges

answered Jun 19, 2013 at 11:34

Khushboo's user avatar

KhushbooKhushboo

2,9952 gold badges22 silver badges23 bronze badges

If you don’t want to change anything and go with «fragment» tag

do this,

<fragment
 android:visibility="gone" (Visibility will not work, just helps in removing frag from xml viewer)(If you want the visibility to be gone make it in your fragment root element visibility=gone)
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:name="com.example.YOUR_FRAGMENT" (This is important)
 />

answered Sep 4, 2018 at 15:34

Sahaj Rana's user avatar

Sahaj RanaSahaj Rana

1,9633 gold badges24 silver badges42 bronze badges

Had the same error type, where exactly the same error message in logcat was displayed. I needed to make changes in the Java Build Path located under Project->Properties. I had included Google Maps related libraries like android-support-v4.jar and google-play-services.jar; however I was missing including these on the 'Build class path' in the 'Order and Export' option menu. Maybe the error lies here.

Try including libraries to the build class path.

The order of which the classes are built might also trigger the error, so my advice is also to try to rearrange the order of the building path. The error disappeared when I used the following order: 'my_project_name/src'->'my_project_name/gen'->'Android Private Libraries'. The last unit contains the jar files mentioned earlier.

JoshDM's user avatar

JoshDM

4,9137 gold badges44 silver badges72 bronze badges

answered Jun 11, 2013 at 22:43

Christian Bøge-Rasmussen's user avatar

If you want to inherit the AppCompatActivity, then you can do something like this-
In the activity xml, use a FrameLayout like this-

<FrameLayout
    android:id="@+id/result_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/progress_frame"/>

and in the activity onCreate

final FragmentManager supportFragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = supportFragmentManager.beginTransaction();
    ft.replace(R.id.result_fragment, fphResultActivityFragment, "result fragment");
    ft.commitAllowingStateLoss();

answered Apr 5, 2016 at 10:51

Vaibhav Sharma's user avatar

Vaibhav SharmaVaibhav Sharma

2,2531 gold badge17 silver badges23 bronze badges

2

I added an id to my fragment that’s it everything works fine. Before this, I changed the manifests file as mentioned above but didn’t solve my error. When you see error try to read it fully you’ll get to know why there is an error. In my case, in the middle of the error trace, it was shown that the fragment id is missing.

<fragment
       android:layout_width="0dp"
       android:layout_weight="2"
       android:layout_height="match_parent"
       class="com.example.mydemofragmentapp.FoodListFragment"
       android:id="@+id/none"/>

answered Nov 1, 2019 at 14:46

ghost deathrider's user avatar

I want to add a possible answer. So here we go.

My situation is, I change my project from Java to Kotlin, and then add Navigation Component. After migrating to Kotlin, then I add the nav_graph.xml. But, after try running the app, I got an inflating error. After checking further and comparing to other projects, turn out My nav_graph.xml did not have app:startDestination="@id/mainFragment". After adding app:startDestination, the error went away and my project runs just fine with no error.

nav_graph.xml

<?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_graph"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="fragment.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" />
</navigation>

So, don’t forget the app:startDestination.

Dharman's user avatar

Dharman

29.3k21 gold badges80 silver badges131 bronze badges

answered May 17, 2021 at 11:35

QuartZ's user avatar

QuartZQuartZ

1543 silver badges11 bronze badges

In your xml file just use a instead of the tag.
The inflater tries to create an android.app.Fragment from which will fail on API < 10.
So you need to create a view group of a different type.

answered May 2, 2013 at 9:37

Lucas de Vil's user avatar

make sure that u have used this one

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBEwmfL0GaZmdVqdTxxxxxxxx-rVgvY" />

not this..

<meta-data 
         android:name="com.google.android.gms.version" 
          android:value="AIzaSyBEwmfL0GaZmdVqdTCvxxxxxxx-rVgvY" /> 

answered Nov 12, 2013 at 8:54

DjP's user avatar

DjPDjP

4,4472 gold badges23 silver badges34 bronze badges

Just had this same error. The reason for mine was the visibility of my fragment class. It was set to default, should be public.

answered Jun 27, 2014 at 15:49

D Kurovskyi's user avatar

D KurovskyiD Kurovskyi

1,05110 silver badges22 bronze badges

Could you post the fragment’s onCreate and onCreateView methods? I had exactly same exception which was caused by an exception in the fragment onCreate method, when I fixed that problem it fixed the Error inflating class fragment.

answered Jul 13, 2014 at 10:16

AdamVe's user avatar

AdamVeAdamVe

3,1362 gold badges17 silver badges12 bronze badges

I was receiving this error in android studio the problem was that my fragment had a relative layout while the code on the OnCreateView function was

mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);

is your code doing the same thing?

Siddharth's user avatar

Siddharth

9,28215 gold badges86 silver badges144 bronze badges

answered Feb 22, 2015 at 19:05

Ahmad Moussa's user avatar

Ahmad MoussaAhmad Moussa

1,28118 silver badges22 bronze badges

Make sure your Activity extends FragmentActivity or AppCompatActivity

answered May 3, 2016 at 6:06

Riyas PK's user avatar

Riyas PKRiyas PK

3,0571 gold badge22 silver badges29 bronze badges

1

Error Description:

Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

  • when we switch via the portrait and the landscape mode and when we use fragments. The xml is:
 <LinearLayout android:id="@+id/mainLayout"
               android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" >

    <ListView android:id="@+id/android:list"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/> 

    <fragment android:id="@+id/fragmentDetails"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              class="de.androidbuch.activiti.task.TaskDetailsFragment"/> 
</LinearLayout>
click below button to copy the code. By — android tutorial — team
  • If we switch via landscape and portrait mode everything works fine. But when we click on the fragment and then switch to the other mode we get the error.
06-21 14:55:05.600: ERROR/AndroidRuntime(7636): FATAL EXCEPTION: main
06-21 14:55:05.600: ERROR/AndroidRuntime(7636): java.lang.RuntimeException: Unable to start activity         
ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}:   android.view.InflateException: Binary XML file line #11: Error inflating class fragment
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3097)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.access$1600(ActivityThread.java:123)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:997)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.os.Looper.loop(Looper.java:126)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.main(ActivityThread.java:3998)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at java.lang.reflect.Method.invoke(Method.java:491)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at dalvik.system.NativeStart.main(Native Method)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.Activity.setContentView(Activity.java:1771)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at de.androidbuch.activiti.task.TaskActivity.onCreate(TaskActivity.java:83)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
06-21 14:55:05.600: ERROR/AndroidRuntime(7636):     ... 12 more

click below button to copy the code. By — android tutorial — team

Solution 1:

  • Make sure to use:
import android.support.v4.app.Fragment;
click below button to copy the code. By — android tutorial — team
  • And also make sure that the Activity that is using the fragment(s) extends FragmentActivity instead of the regular Activity to get the FragmentActivity class.
import android.support.v4.app.FragmentActivity;
click below button to copy the code. By — android tutorial — team

Solution 2:

  • The exception android.view.InflateException: Binary XML file line: #… Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can’t rely on that.
  • For instance the next pattern is wrong:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) 
{
    final View view = inflater.inflate(R.layout..., container, false);

    Button button = getActivity().findViewById(R.id...);
    button.setOnClickListener(...); - another problem: button is null

    return view;
}
click below button to copy the code. By — android tutorial — team

Pattern #1

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

    Button button = view.findViewById(R.id...);
    button.setOnClickListener(...);

    return view;
}

click below button to copy the code. By — android tutorial — team

Solution 3:

  • Make sure your Activity extends FragmentActivity.

Solution 4:

  • The solution for is the order of super.onCreate and setContentView within the FragmentActivity
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fc_activity_list_profiles);
click below button to copy the code. By — android tutorial — team

Содержание

  1. [Solved-4 Solutions] Error inflating class fragment
  2. Error Description:
  3. Solution 1:
  4. Solution 2:
  5. Pattern #1
  6. Solution 3:
  7. Solution 4:
  8. Related Searches to Error inflating class fragment
  9. Error inflating class fragment android studio

[Solved-4 Solutions] Error inflating class fragment

Error Description:

We get the Error

Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

  • when we switch via the portrait and the landscape mode and when we use fragments. The xml is:
click below button to copy the code. By — android tutorial — team
  • If we switch via landscape and portrait mode everything works fine. But when we click on the fragment and then switch to the other mode we get the error.
click below button to copy the code. By — android tutorial — team

Solution 1:

click below button to copy the code. By — android tutorial — team
  • And also make sure that the Activity that is using the fragment(s) extends FragmentActivity instead of the regular Activity to get the FragmentActivity class.
click below button to copy the code. By — android tutorial — team

Solution 2:

  • The exception android.view.InflateException: Binary XML file line: #. Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can’t rely on that.
  • For instance the next pattern is wrong:
click below button to copy the code. By — android tutorial — team

Pattern #1

click below button to copy the code. By — android tutorial — team

Solution 3:

  • Make sure your Activity extends FragmentActivity.

Solution 4:

  • The solution for is the order of super.onCreate and setContentView within the FragmentActivity
click below button to copy the code. By — android tutorial — team

World’s No 1 Animated self learning Website with Informative tutorials explaining the code and the choices behind it all.

Источник

Error inflating class fragment android studio

i am using above layout and i am getting same error (Layout inflation error).Please let me know where i am wrong.

There are other reasons for getting such an error- not just in the misspelling of a widget name or tool. I got that error when I used a drawable file (containing a color I created using xml code) as my textcolor. It appears errors generated can cover a truckload of reasons and not just one reason.

im also getting this error

The mobile version works great, but the tablet version gives the above error. tablet layout:

EDIT, something went wrong and the post has been posted 3 times

Repeated, sorry about that

I was custom styling the buttons by using custom xml file inside the drawable folder. I was getting the following error: Binary XML file line #1: Error inflating class button

This issues almost made me break my head.

But I resolved it. I believe it happens because either the XML is not properly formatted (missing OR spelling mistakes) or incorrect xmlns:android URL.

Also make sure please that in .axml file, when you specify the xml as background or whatever, use this format: android:background=»@drawable/yourxmlfilenamewithoutfiletype»_

Thanks and Cheers!

In my case this exception was caused by the package name. Just in case someone else has the same issue you can find below what I found at the very bottom of the following link:

It is very important to remember that when adding a fragment to a layout file, that Android expects the package name to be lower-case. If the package name is upper-case then an Android.Views.InflateException will be thrown.

In my case, the problem was caused by the fact that I failed to return the correct value. I believe the line with the return below was auto generated for me, and as a result I didn’t look at it too closely and didn’t notice that it was returning the wrong thing.

All I had to do was return the view:

I hope this helps someone.

I’ve got same issue from Fabric/Crashlytics.

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class

my axml file is following

** using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Text;

using RoadRunner.Shared; using RoadRunner.Shared.Classes;

I’ve wasted many time with this issue but couldn’t find any solution in anywhere.

I’ve attached above .AXML and .cs files.

I’m new to Xamarin, I got the same error, I was trying to make a Maps fragment in the app, can someone help me?

The way i fixed this error was clean and rebuild. And the rebuild might take longer than it should, try it out, worked for me

Sorry posted at wrong window

Hi! I have the same problem -> Android.Views.InflateException: Binary XML file line #1: Error inflating class GrumsonLed

I don’t know what is wrong! Can someone please help me!

Источник

Mobile Information: ‘model: QMobile T50/version: 4.4.3/sdk: 19’

Thread Name: ‘main’
Back traces starts.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infreewifi.cct/com.mobile.freewifi.activity.WifiMapActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1225)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5285)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:295)
at android.app.Activity.setContentView(Activity.java:1947)
at com.mobile.freewifi.activity.BaseMapActivity.onCreate(BaseMapActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5341)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
… 11 more
Caused by: java.lang.NullPointerException
at ijn.a(:com.google.android.gms:1227)
at ijm.b(:com.google.android.gms:197)
at gzr.logEvent(:com.google.android.gms:1216)
at gzr.logEvent(:com.google.android.gms:60)
at com.google.android.chimera.container.DebugLogger.logEvent(:com.google.android.gms:65)
at com.google.android.chimera.container.ConfigurationManager.loadModuleByModuleId(:com.google.android.gms:546)
at gzt.a(:com.google.android.gms:74)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms:1112)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:95)
at ruh.onTransact(:com.google.android.gms:62)
at android.os.Binder.transact(Binder.java:384)
at com.google.android.gms.maps.internal.zzc$zza$zza.zzag(Unknown Source)
zzbpf
at com.google.android.gms.maps.SupportMapFragment$zzb.zzbow(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
setActivity
getMapAsync
zza
at com.google.android.gms.dynamic.zza.zzbbt(Unknown Source)
zznd
zza
zza
onInflate
onCreate
onCreateView
zza
zza
zza
zza
at com.google.android.gms.dynamic.zza.zzbbt(Unknown Source)
zznd
zza
zza
onInflate
onCreate
onCreateView
zza
zza
zza
zza
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1142)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2287)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
onCreateView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
… 22 more


<include layout="@layout/common_title_layout" />

<LinearLayout
    android:id="@+id/map_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="visible">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </fragment>
</LinearLayout>

<RelativeLayout
    android:visibility="gone"
    android:id="@+id/check_gps_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_f6f6f6"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        >
        <ImageView
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:src="@drawable/ic_map_setting_location"
            android:layout_gravity="center"
            />
        <TextView
            android:id="@+id/map_oops"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="12dp"
            android:textSize="12sp"
            android:textColor="@color/color_889499"
            android:text="@string/map_cannot_get_your_location"/>
        <TextView
            android:id="@+id/map_setting_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="6dp"
            android:textSize="12sp"
            android:textColor="@color/color_889499"
            android:text="@string/map_check_gps"
            />
        <Button
            android:id="@+id/map_setting"
            android:layout_width="160dp"
            android:layout_height="36dp"
            android:layout_gravity="center"
            android:layout_marginTop="48dp"
            android:background="@drawable/map_setting_btn_selector"
            android:gravity="center"
            android:text="@string/setting"
            android:textColor="@drawable/connect_txt_selector"
            android:textSize="12sp" />


    </LinearLayout>
</RelativeLayout>

Probability of occurrence 0.001

  • Remove From My Forums
  • Question

  • User1763 posted

    Hi,

    My code is

    Main.axml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/MyButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Hello" />
        <fragement
            android:name="test.fragement.example.Fragment1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout> 
    

    and Activity1.cs

    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);          
        }
    }
    

    and Fragment1.cs

    namespace test.fragement.example
    {
        public class Fragment1 : Fragment
        {
            public override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
            }
            public override View OnCreateView(LayoutInflater inflater, ViewGroup grp, Bundle bundle)
            {
                base.OnCreateView(inflater, grp, bundle);
                View vw = inflater.Inflate(Resource.Layout.frag1, grp, true);
                return vw;
            }
        }
    }
    

    This code is throwing an error for me. Stack trace

    Android.Views.InflateException: Binary XML file line #1: Error inflating class fragement
      at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.4-series/6418373f/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:616
      at Android.App.Activity.SetContentView (int) [0x0006b] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.4-series/6418373f/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.App.Activity.cs:3119
      at test.fragement.example.Activity1.OnCreate (Android.OS.Bundle) [0x00009] in c:Monotest.fragement.exampletest.fragement.exampleActivity1.cs:22
      at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.4-series/6418373f/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.App.Activity.cs:1490
      at (wrapper dynamic-method) object.4b6fec41-0b84-4cbb-85e3-f821d04add6e (intptr,intptr,intptr) <IL 0x00017, 0x00043>
      --- End of managed exception stack trace ---
      android.view.InflateException: Binary XML file line #1: Error inflating class fragement
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
        at android.app.Activity.setContentView(Activity.java:1657)
        at test.fragement.example.Activity1.n_onCreate(Native Method)
        at test.fragement.example.Activity1.onCreate(Activity1.java:28)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
        at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.ClassNotFoundException: android.view.fragement in loader dalvik.system.PathClassLoader[/data/app/test.fragement.example-1.apk]
        at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at android.view.LayoutInflater.createView(LayoutInflater.java:471)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
        ... 21 more
    

    How to solve this ?

Answers

  • User7668 posted

    What a numpty. I have put the namespace in the path for the classname now and of course it works. Sorry for wasting everyone’s time

    • Marked as answer by

      Thursday, June 3, 2021 12:00 AM

I couldn’t solve my problem using provided answers. Finally I changed this:

<fragment
android:id="@+id/fragment_food_image_gallery"
android:name="ir.smartrestaurant.ui.fragment.ImageGalleryFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout="@layout/fragment_image_gallery"
tools:layout="@layout/fragment_image_gallery" />

to this :

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="200dp" />

,

private void showGallery() {
    ImageGalleryFragment fragment = new ImageGalleryFragment()
    getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }

and it works.
If you are using it inside fragment, use getChildFragmentManager instead of getSupportFragmentManager.

TL/DR: An exception occurred during the creation of a fragment referenced from a higher-level layout XML. This exception caused the higher-level layout inflation to fail, but the initial exception was not reported; only the higher-level inflation failure shows up in the stack trace. To find the root cause, you have to catch and log the initial exception.


The initial cause of the error could be a wide variety of things, which is why there are so many different answers here as to what fixed the problem for each person. For some, it had to do with the id, class, or name attributes. For others it was due to a permissions issue or a build setting. For me, those didn’t fix the problem; instead there was a drawable resource that existed only in drawable-ldrtl-xhdpi, instead of in an applicable place like drawable.

But those are just details. The big-picture problem is that the error message that shows up in logcat doesn’t describe the exception that started it all. When a higher-level layout XML references a fragment, the fragment’s onCreateView() is called. When an exception occurs in a fragment’s onCreateView() (for example while inflating the fragment’s layout XML), it causes the inflation of the higher-level layout XML to fail. This higher-level inflation failure is what gets reported as an exception in the error logs. But the initial exception doesn’t seem to travel up the chain well enough to be reported.

Given that situation, the question is how to expose the initial exception, when it doesn’t show up in the error log.

The solution is pretty straightforward: Put a try/catch block around the contents of the fragment’s onCreateView(), and in the catch clause, log the exception:

public View onCreateView(LayoutInflater inflater, ViewGroup contnr, Bundle savedInstSt) {
    try {
        mContentView = inflater.inflate(R.layout.device_detail_frag, null);
        // ... rest of body of onCreateView() ...
    } catch (Exception e) {
        Log.e(TAG, "onCreateView", e);
        throw e;
    }
}

It may not be obvious which fragment class’s onCreateView() to do this to, in which case, do it to each fragment class that’s used in the layout that caused the problem. For example, in the OP’s case, the app’s code where the exception occurred was

at android.app.Activity.setContentView(Activity.java:1901)

which is

   setContentView(R.layout.activity_main);

So you need to catch exceptions in the onCreateView() of any fragments referenced in layout activity_main.

In my case, the root cause exception turned out to be

Caused by: android.content.res.Resources$NotFoundException: Resource
   "com.example.myapp:drawable/details_view" (7f02006f)  is not a
   Drawable (color or path): TypedValue{t=0x1/d=0x7f02006f a=-1
   r=0x7f02006f}

This exception didn’t show up in the error log until I caught it in onCreateView() and logged it explicitly. Once it was logged, the problem was easy enough to diagnose and fix (details_view.xml existed only under the ldrtl-xhdpi folder, for some reason). The key was catching the exception that was the root of the problem, and exposing it.

It doesn’t hurt to do this as a boilerplate in all your fragments’ onCreateView() methods. If there is an uncaught exception in there, it will crash the activity regardless. The only difference is that if you catch and log the exception in onCreateView(), you won’t be in the dark as to why it happened.

P.S. I just realized this answer is related to @DaveHubbard’s, but uses a different approach for finding the root cause (logging vs. debugger).

After long time for debugging, I have fixed this problem. (Although I still cannot explain why). That I change property android:name to class. (although on Android Document, they say those properties are same, but it works !!!)

So, it should change from :

 android:name="com.fragment.NavigationDrawerFragment"

to

class = "com.fragment.NavigationDrawerFragment"

So, new layout should be :

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->

<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    class = "com.fragment.NavigationDrawerFragment" />

Hope this help :)

I updated navigation component to 2.3.2 version.

In the 2.3.1 version work normally

I was going through the documentation and I did not find anything about the error or about any update in the component

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.m.marketplace, PID: 16698
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m.marketplace/com.m.marketplace.ui.MainActivity}: android.view.InflateException: Binary XML file line #11 in com.m.marketplace:layout/activity_main: Binary XML file line #11 in com.m.marketplace:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7590)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: android.view.InflateException: Binary XML file line #11 in com.m.marketplace:layout/activity_main: Binary XML file line #11 in com.m.marketplace:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #11 in com.m.marketplace:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
     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.navigate(NavController.java:1050)
        at androidx.navigation.NavController.navigate(NavController.java:942)
        at com.m.marketplace.utils.ktx.NavControllerKt.navigateSafe(NavController.kt:17)
        at com.m.marketplace.utils.ktx.NavControllerKt.navigateSafe$default(NavController.kt:13)
        at com.m.marketplace.utils.ktx.FragmentKt.navigate(Fragment.kt:15)
        at com.m.marketplace.ui.login.view.PreviousLoginDialog.goToHome(PreviousLoginDialog.kt:119)
        at com.m.marketplace.ui.login.view.PreviousLoginDialog.access$goToHome(PreviousLoginDialog.kt:28)
        at com.m.marketplace.ui.login.view.PreviousLoginDialog$firstInit$1.invoke(PreviousLoginDialog.kt:111)
        at com.m.marketplace.ui.login.view.PreviousLoginDialog$firstInit$1.invoke(PreviousLoginDialog.kt:28)
        at com.m.marketplace.ui.login.view.PreviousLoginDialog.onCreate(PreviousLoginDialog.kt:44)
        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.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    E/AndroidRuntime:     at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
        at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2571)
        at androidx.fragment.app.Fragment.onCreate(Fragment.java:1685)
        at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:264)
        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.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:166)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
        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.tryCreateView(LayoutInflater.java:1073)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1001)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
        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:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.m.marketplace.ui.MainActivity.onCreate(MainActivity.kt:50)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7590)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

4 Answers

I was facing the same problem and I figured out that Android studio is not adding name tags in navigation graph. So, adding name tag in navigation graph xml worked for me.

In nav_graph.xml

<fragment
    android:id="@+id/loginFragment"
    android:name="com.example.MyApp.fragments.LoginFragment"
    android:label="fragment_login"
    tools:layout="@layout/fragment_login">

Or if you are not sure then switch to the design and in attributes tab on right side select the class name from drop-down list

Name Tag in Design View

Same issue here with navigation component 2.3.2 version.

In the 2.3.1 version works normally

If you’re using Jetpack Compose change MainActivity extends to AppCompatActivity instead of CompatActivity

First make sure you have added ID to your nav graph and fragment container view. Also check fragment (declared asstart destination) does not have any instantiation issue.

Home
>
Android
>
Detail page

In the use of the fragment, sometimes this problem occurs:

Android.view.InflateException: Binary XML file line #**: Binary XMLfile line #**: Error inflating class fragment

Mainly in the following situations:

1, Fragment should be «android.support.v4.app.Fragment;»

2, Activity must inherit FragmentActivity

import android.support.v4.app.FragmentActivity;

3, «android:name» of fragment in xml must be full path of fragment

4, when using support.v4 package of version 6.0, need to set «android:id», otherwise, the above error will occur.

Posted by fighting71k
in Android
at Apr 15, 2017 — 9:45 AM
Tag:
Fragment

Posted By: Anonymous

I have a very frustrating error that I cannot explain. I created an Android application that uses Android AppCompat to make it compatible with older versions. Here is my main activity layout file:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->

    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.fragment.NavigationDrawerFragment" />

</android.support.v4.widget.DrawerLayout>

And here is main code of my activity :

public class MainActivity extends ActionBarActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   }
}

The main problem here is : above code run smoothly on almost devices (stimulated device, or some real devices). But when I run it on Samsung S3. It notices this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{view.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
            at android.app.ActivityThread.access$700(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4856)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
            at android.app.Activity.setContentView(Activity.java:1901)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)

Please tell me how to fix error, thanks 🙂

Solution

After long time for debugging, I have fixed this problem. (Although I still cannot explain why). That I change property android:name to class. (although on Android Document, they say those properties are same, but it works !!!)

So, it should change from :

 android:name="com.fragment.NavigationDrawerFragment"

to

class = "com.fragment.NavigationDrawerFragment"

So, new layout should be :

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->

<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    class = "com.fragment.NavigationDrawerFragment" />

Hope this help 🙂

Answered By: Anonymous

Related Articles

  • Unable to run Robolectric and Espresso with a…
  • MaterialCardView is in front of NavigationView. How can I…
  • Error in styles_base.xml file — android app — No resource…
  • Android : Fragment doesn’t fit into Fragment Container
  • Error inflating class android.support.v7.widget.Toolbar?
  • Error inflating class…
  • Navigation View and Constraint Layout
  • Failed to load AppCompat ActionBar with unknown error in…
  • Same Navigation Drawer in different Activities
  • How to implement HorizontalScrollView like Gallery?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Перейти к содержанию

На чтение 2 мин Обновлено 15.01.2023

[Solved-4 Solutions] Error inflating class fragment

Error Description:

We get the Error

Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

  • when we switch via the portrait and the landscape mode and when we use fragments. The xml is:
click below button to copy the code. By — android tutorial — team
  • If we switch via landscape and portrait mode everything works fine. But when we click on the fragment and then switch to the other mode we get the error.
click below button to copy the code. By — android tutorial — team

Solution 1:

click below button to copy the code. By — android tutorial — team
  • And also make sure that the Activity that is using the fragment(s) extends FragmentActivity instead of the regular Activity to get the FragmentActivity class.
click below button to copy the code. By — android tutorial — team

Solution 2:

  • The exception android.view.InflateException: Binary XML file line: #. Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can’t rely on that.
  • For instance the next pattern is wrong:
click below button to copy the code. By — android tutorial — team

Pattern #1

click below button to copy the code. By — android tutorial — team

Solution 3:

  • Make sure your Activity extends FragmentActivity.

Solution 4:

  • The solution for is the order of super.onCreate and setContentView within the FragmentActivity
click below button to copy the code. By — android tutorial — team

Related Searches to Error inflating class fragment

World’s No 1 Animated self learning Website with Informative tutorials explaining the code and the choices behind it all.

Источник

Понравилась статья? Поделить с друзьями:
  • Error inflating class fragment android kotlin
  • Error inflating class edittext
  • Error inflating class com google android material navigation navigationview
  • Error inflating class com google android material button materialbutton
  • Error inflating class com google android material bottomnavigation bottomnavigationview