未显示 Android Studio 预览

标签 android android-studio android-layout android-fragments android-studio-3.6.1

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)


显示此消息并且布局预览未显示在基本 Activity 或其他模板接受空 Activity 中。

在此消息之前,显示了一条关于将 fragment 标记替换为 fragmentcontainerview 的消息.我解决了这个问题,然后显示了上述消息。我已经尝试过已知的解决方案,如重建、刷新布局和无效缓存/重新启动等,但他们没有成功。

最佳答案

如果您查看 FragmentContainerView 的源代码,你会看到:

/**
 * 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.");
}

不幸的是,这是布局预览调用的构造函数。在 Android Studio 团队修复此问题之前,您似乎无能为力。

这是系统“应该”使用的构造函数:
FragmentContainerView(Context context, AttributeSet attrs, FragmentManager fm) {
    // ...
}

如果您实际运行您的应用程序,应该调用它并且一切都应该正常工作。

关于未显示 Android Studio 预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60674505/

相关文章:

android - Gradle 无法构建 ActionBarSherlock

java - Java Android Studio 新手,需要一些指导

java - 在不使用 fragment 的情况下在 Android 中显示 Google map ?

android - AsyncLayoutInflator 陷阱

android - 在持续集成系统 Hudson 中编译 Android 项目?

android - 方向更改时列表 fragment 中 ListView 行的重复

android - 使用类的 Sqlite 数据未显示在回收器列表中

android - Samsung Galaxy Camera Android API - 自定义相机应用程序

android - OpenSL ES - 安卓工作室 - NDK 10R

android - 我可以使用 layout_weight 来定位 RelativeLayout 吗?