174

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
  • 1,618
  • 3
  • 27
  • 41
tsync
  • 2,257
  • 4
  • 16
  • 15
  • Are you keeping separate layout files in your layout and layout-land folders? – Nathan Fig Jun 21 '11 at 12:30
  • Can you provide the full error log, including the lines *after* "Unable to start activity..." – Dave Jun 21 '11 at 12:46
  • @Nathan Fig: yes I do.. but getting the errors.. – tsync Jun 21 '11 at 12:52
  • @Dave: here you go.. any idea? – tsync Jun 21 '11 at 13:03
  • Need landscape layout code, and also helpful would be your calls to setContentView and also how you are firing off activity in portrait mode as I'm assuming you are. At the moment there doesn't seem to be enough information. Looking at FragmentLayout example in API demos may help you as it sounds like you are doing something similar. – PJL Jun 21 '11 at 16:04
  • Refer this link you will get an idea.. [link][1] [1]: http://stackoverflow.com/a/16271463/3630904 – newbie May 15 '14 at 04:27
  • this link solved this exact same problem for us: [here...](http://stackoverflow.com/questions/34532017/viewpagerindicator-library-doesnt-work-in-4-4) – OZG Jul 21 '16 at 10:42
  • for Android beginners. note that **very simply, this can be caused by** having in your XML ** – Fattie May 07 '17 at 15:09
  • use framelayout instead of fragment. – Kundan Feb 13 '18 at 13:28
  • [I think this issue is solved here at least for me](https://stackoverflow.com/a/60597670/9513504) – thegirlincode May 28 '21 at 13:44

43 Answers43

132

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
  • 660
  • 7
  • 19
Dandre Allison
  • 5,795
  • 5
  • 39
  • 55
  • 14
    This got upvoted a lot, but for me it crashes when resuming the app, and I am already using v4 Fragment and v4 FrsgmentActivity - to be more precise AppCompatActivity. – joseph Jun 27 '15 at 21:00
  • 4
    This is no solution. import android.support.v4.app.Fragment; does not change anything. Actually it is even unused and unneeded import. – f470071 Nov 04 '15 at 12:44
  • 8
    Thanks, a side note is that AppCompatActivity is a descendant of FragmentActivity. – abedfar Mar 03 '16 at 20:52
  • but i am using dagger fragment , and dagger fragment always extend fragment not fragment activity – user3475052 Jun 25 '19 at 06:03
103

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(...);
}
Grigori A.
  • 2,528
  • 1
  • 19
  • 19
54

Make sure your Activity extends FragmentActivity.

james
  • 25,347
  • 17
  • 92
  • 109
  • 2
    [Note: FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity.](https://developer.android.com/training/basics/fragments/creating.html) – Munib Jul 13 '17 at 23:03
  • 1
    @RobinF AppCompatActivity extends itself FragmentActivity – Nicola Gallazzi Sep 16 '19 at 09:57
39

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.

Matiss
  • 4,227
  • 2
  • 24
  • 26
22

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);
Wilko
  • 240
  • 1
  • 4
  • 1
    In a JAVA method, `super` must always be called at the beginning of the method. – Stéphane Bruckert Mar 14 '13 at 22:27
  • 11
    @StéphaneBruckert it is not always the case that `super.method()` must be called first. It depends on what the super class's method does. In the case of `FragmentActivity` it does some initialization needed to make `setContentView` work properly. However in another method (say `onKeyUp`) you may want to conditionally do some work with the action, but fallback onto `super.onKeyUp` in which case you will very likely want to call `super.onKeyUp` at the very end of the function. – CoatedMoose Mar 25 '13 at 03:09
  • It is probably also worth noting that `super()` (the superclass's constructor) must be the first function called in any child classes constructor, however calling it is not necessarily mandatory (it would depend entirely on the class being extended). – CoatedMoose Mar 25 '13 at 03:11
17

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.

Dhiraj Gupta
  • 7,159
  • 4
  • 43
  • 50
16

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;
hdemirchian
  • 161
  • 1
  • 2
  • Thanks, This solution worked for me too. I was struggling with this issue for 2 days. Finally your solution worked for me.Thanks hdemirchian.. – Lakshmanan Jun 14 '13 at 05:42
  • You are awesome! My problem was opposite - the Fragment wizard imports android.support.v4.app.Fragment by default. And my main activity extends android.app.Activity. – Industrial Control Freak Feb 11 '18 at 05:11
12

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
  • 17
  • 4
Lei
  • 606
  • 1
  • 5
  • 9
  • Just to add what Lei is saying is, put try catch around code inside your oncreateview and find out actual root cause. Thanks Lei! – Swaroop Feb 08 '16 at 10:42
12

Have you tried:

 <fragment
            android:name="de.androidbuch.activiti.task.TaskDetailsFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
Blundell
  • 69,653
  • 29
  • 191
  • 215
6

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.

mask
  • 5,808
  • 3
  • 21
  • 22
6

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
  • 6,624
  • 16
  • 47
  • 56
xMythicx
  • 767
  • 1
  • 9
  • 26
6

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
  • 19,479
  • 11
  • 78
  • 93
Suryavel TR
  • 3,062
  • 1
  • 18
  • 23
5

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

Gil
  • 568
  • 5
  • 9
4

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.

4

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?

Nathan Fig
  • 14,103
  • 9
  • 40
  • 56
  • Thanks for your answer.. well I only have a fragment in the landscape mode and not in the portrait mode. So I can click only in the landscape mode to see the fragment. And after I switch again to the landscape mode I get this error... – tsync Jun 21 '11 at 13:27
  • That's almost certainly the problem then- you have a view in portrait mode that can't be found in landscape mode. Does adding the fragment to landscape fix the issue? – Nathan Fig Jun 21 '11 at 15:25
  • 1
    You can have views that don't exist in both files -- that's the whole point of having separate -land and -port layouts. However, check the ids-- I had the id missing in my portrait file and was getting this error when switching. – maxpolun Jul 20 '12 at 19:56
3

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
  • 6,624
  • 16
  • 47
  • 56
Khushboo
  • 2,817
  • 2
  • 20
  • 21
3

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.

oddmeter
  • 754
  • 7
  • 15
3

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)
 />
Sahaj Rana
  • 1,773
  • 3
  • 19
  • 38
2

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
  • 4,838
  • 7
  • 42
  • 70
1

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.

1

I got this error when using a ListFragment but the list view id was listView1 instead of list.

'Your content must have a ListView whose id attribute is 'android.R.id.list''

http://developer.android.com/reference/android/app/ListFragment.html

Eric Woodruff
  • 5,907
  • 3
  • 31
  • 32
1

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" /> 
DjP
  • 3,957
  • 2
  • 21
  • 30
1

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

southerton
  • 992
  • 8
  • 20
1

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.

AdamVe
  • 2,826
  • 2
  • 16
  • 12
1

I had a similar problem; after running the AdMob example, I tried to insert Ads in my app, causing this error:

01-02 16:48:51.269    8199-8199/it.dndc.BreathPlot E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
 Caused by: android.view.InflateException: Binary XML file line #57: Error inflating class fragment

The solution is: you cannot insert the fragment for an Ad into a ListActivity. Instead, I could add it to a FragmentActivity and to an ActionBarActivity without any problem.

My suggestion is: start from the AdMob example and add into it your existing app: I would have saved a lot of time !!!

approxiblue
  • 6,624
  • 16
  • 47
  • 56
user2270629
  • 144
  • 8
1

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
  • 8,842
  • 13
  • 79
  • 133
Ahmad Moussa
  • 1,128
  • 16
  • 21
1

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();
Vaibhav Sharma
  • 2,113
  • 1
  • 15
  • 21
1

Make sure your Activity extends FragmentActivity or AppCompatActivity

Riyas PK
  • 2,467
  • 1
  • 19
  • 26
1

I got the same error, but my issue was that my fragment did not have an id in the xml layout of the parent activity.

stevethecollier
  • 886
  • 8
  • 12
1

After one day struggle i found some scenario check may be you are facing same,

If everything is woking same as google code then please check manifest file in my case i added geo key and map key that's why exception occurs,

Note - do not add two keys in manifest file remove map key

meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key"/>

above code and add this code.

 <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/auto_location"/>

 <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
Yogesh Rathi
  • 5,684
  • 4
  • 42
  • 71
1

If you use obfuscation with Navigation component you need to exclude android's fragment and your args. Add these lines to your proguard-rules files:

# Exclude the fragments and argType for navigation component.
-keep class * extends androidx.fragment.app.Fragment{}
-keep class com.safetonet.presentation.features.parent.adddevice.model.PresentableAddDeviceData
user3193413
  • 385
  • 4
  • 9
1

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"/>
0

If your fragment is a List Fragment then the activity which uses that fragment must implement the onFragmentInteraction interface.

Undo
  • 25,204
  • 37
  • 102
  • 124
0

In my case,

...
android.view.InflateException: 
Binary XML file line #24: Error inflating class fragment
...
Caused by: java.lang.InstantiationException: 
class *.HomeFragment has no zero argument constructor
...

After I added the empty constructor, it's resolved.

public HomeFragment() {}
TonnyTao
  • 472
  • 5
  • 7
0

My simple approach

If you use <fragment> in your xml then you must provide name attribute i.e;

      <fragment 
        android:name='path.to.your.fragment
        />`

which will initialise your fragment and you don't need to do it from java.

If you want to use Java approach to add fragment then, use any layout like

<LinearLayout
   android:id="@+id/fragment_container"
 />

and use this container for your fragment

transaction.replace(R.id.fragment_container, fragment_obj);
Santosh Kumar
  • 145
  • 1
  • 5
0

Make sure You have put your google-services.json on your respective folder and oviously add

<meta-dataandroid:name="com.google.android.geo.API_KEY" android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />

in your manifest file. Check your gradle with "implementation 'com.google.android.gms:play-services-maps:15.0.1'"

It will work.

0

Here is my solution to this problem.

Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #31: Duplicate id 0x7f09006d, tag null, or parent id 0xffffffff with another fragment for com.example.eduardf.audit.DateTime

In my case, the error occurred when re-opening the DialogFragment with a Fragment.

<android.support.constraint.ConstraintLayout
    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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/ic_black_edit_24px"
            android:singleLine="true" />
        <fragment
            android:id="@+id/fragment_date"
            android:name="com.example.eduardf.audit.DateTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment_date_time" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

DailogFragment stores the fragment after the first inflate. To prevent this from happening, I forcibly deleted the fragment.

@Override
public void onDestroyView () {
    if (!(afterRotate || getActivity() == null)) {
        final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        final Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_date);
        if (fragment != null)
            fragmentManager.beginTransaction().remove(fragment).commit();
    }
    super.onDestroyView();
}

Also had to take care of unnecessary removal of the fragment, for example, when you rotate the screen.

private boolean afterRotate = false;
...
@Override
public void onSaveInstanceState (Bundle outState) {
    super.onSaveInstanceState(outState);
    afterRotate = true;
}
Eric Aya
  • 68,765
  • 33
  • 165
  • 232
0

I did not have the androidx.fragment:fragment dependency.

You can get the latest version from the Jetpack/ AndroidX docs:

dependencies {
    def fragment_version = "1.2.5"

    // Java language implementation
    implementation "androidx.fragment:fragment:$fragment_version"
    // Kotlin
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    // Testing Fragments in Isolation
    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
}
Ben Butterworth
  • 6,030
  • 1
  • 28
  • 48
0

For me, refactored code caused the problem. I moved a fragment into a new package and one of the references in the xml file caused the bug. So inspect your code very well

Kolaaa
  • 198
  • 2
  • 6
0

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
  • 21,838
  • 18
  • 57
  • 107
QuartZ
  • 136
  • 2
  • 11
0

My problem was that I am adding Google maps API key in the value folder! move it to src/debug/res/values/sensitive_keys.xml

Amr
  • 141
  • 2
  • 10
-1

This is one of the errors you could get If your activity is not registered on the manifest. Check and ensure that your activity is registered on the manifest, that could be the possible cause of your error.

Peter Akwa
  • 61
  • 1
  • 5
  • 1
    Would you like trying to rephrase this into something which gives more an impression of being a helpful answer and less of "Me too." post? I think it is possible. – Yunnosch Sep 05 '19 at 21:47
-9

Adding this to manifest solved my problem

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Akshay
  • 37
  • 2
  • 10