Android studio preview not shown
Asked Answered
E

2

6

The following classes could not be instantiated: androidx.fragment.app.FragmentContainerView (Open Class, Show Exception, Clear Cache)

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.

Exception Details:

java.lang.UnsupportedOperationException: FragmentContainerView must be within a FragmentActivity to be instantiated from XML. 
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:117) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)  
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)  
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)  
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:501)

This message was shown and layout preview was not shown in basic activity or other template accept empty activity.

Before this message, one message was shown about replacing fragment tag with fragmentcontainerview. I fixed this and then the above message was shown. I have tried known solutions like rebuild, refresh layout and invalidate caches/restart, etc, but they didn't do the trick.

Emotionalize answered 13/3, 2020 at 17:0 Comment(0)
H
3

If you look at the source code for FragmentContainerView, you'll see this:

/**
 * Do not call this constructor directly. Doing so will result in an
 * {@link UnsupportedOperationException}.
 */
public FragmentContainerView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    throw new UnsupportedOperationException("FragmentContainerView must be within a "
            + "FragmentActivity to be instantiated from XML.");
}

Unfortunately, this is the constructor that the layout preview invokes. It doesn't seem that there's anything you can do about this error until the Android Studio team fixes this issue.


This is the constructor the system is "supposed" to use:

FragmentContainerView(Context context, AttributeSet attrs, FragmentManager fm) {
    // ...
}

If you actually run your app, this should be invoked and everything should work just fine.

Hyperpyrexia answered 13/3, 2020 at 17:9 Comment(1)
Yeah you write I run my app it successful run but layout preview not show.i report this error to android developer team. But I don't known in what time they solve it .Emotionalize
P
2

Came across the same problem today. Check this out: https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha01.

They've noticed this problem and released a newer version that fixes this bug.

I tried to use this version of the Fragment library, turned out there is no error anymore, but still the fragments ain't displayed in the layout preview. Not sure if that's my mistake or they are still working on it.

Update:

To use a library or to specify a specific version of a library:

open the file build.gradle (Module: app), add the following line in the dependency section:

implementation '[library_name]:[version]'

For example, if you want to use version 1.3.0-alpha01 of the fragment library, add this line:

implementation 'androidx.fragment:fragment:1.3.0-alpha01'

In case you want to find the name of each library, check this out: https://developer.android.com/jetpack/androidx/migrate/artifact-mappings

To find out the current release and latest update of the libraries: https://developer.android.com/jetpack/androidx/versions/

Phantasy answered 16/3, 2020 at 9:49 Comment(2)
Can directly change fragment library by just adding in file or are in setting please tell.Emotionalize
@ShivamUpadhyay I updated my answer. Let me know if you still need help.Phantasy

© 2022 - 2024 — McMap. All rights reserved.