Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio preview not shown

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.

like image 989
Shivam Upadhyay Avatar asked Mar 13 '20 17:03

Shivam Upadhyay


People also ask

Can t see preview in Android Studio?

Go to File -> Settings -> Apperance & Behavior -> System Settings -> Android SDK : Check Show Package Details which is located on the bottom right. Under the latest Android SDK version (for ex: Android 8.1 (Oreo) ), check Google Play Intel x86 Atom System Image. Now click apply button.

Where is the preview pane in Android Studio?

If you want to see the live preview, in the right part of the screen you should have a button call Preview that show/hide the live preview. If what you want is to use the WYSISYG editor mode, in the bottom of the editor there is a tab that switch between XML mode and WYSISYG mode.

Why Design is not showing in Android Studio?

It depends on your version. Here is a better way: open project and right click on app then go to open module settings click on dependencies tab and then click + icon on right then click 1st option which is library dipendency choose your dependencies click ok.

What is preview Android Studio?

Preview builds give you early access to new features in all aspects of the IDE, plus early versions of other tools such as the Android Emulator and platform SDK previews.


2 Answers

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.

like image 64
Ben P. Avatar answered Oct 05 '22 07:10

Ben P.


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/

like image 39
Patricia Hu Avatar answered Oct 05 '22 08:10

Patricia Hu