【问题标题】:Android studio preview not shown未显示 Android Studio 预览
【发布时间】:2020-03-13 17:00:09
【问题描述】:

无法实例化以下类: androidx.fragment.app.FragmentContainerView(开放类,显示异常,清除缓存)

提示:在自定义视图中使用 View.isInEditMode() 可跳过代码或在 IDE 中显示示例数据。如果这是一个意外错误,您也可以尝试构建项目,然后手动刷新布局。

异常详情:

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)

此消息已显示,布局预览未显示在基本活动或其他模板接受空活动。

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

【问题讨论】:

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


    【解决方案1】:

    如果您查看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 开发团队。但我不知道他们什么时候解决它。
    【解决方案2】:

    今天遇到了同样的问题。看看这个:https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha01

    他们注意到了这个问题并发布了一个更新的版本来修复这个错误。

    我尝试使用这个版本的片段库,结果没有错误了,但是片段仍然没有显示在布局预览中。不确定这是我的错误还是他们仍在努力。

    更新

    要使用库或指定库的特定版本:

    打开文件build.gradle (Module: app),在dependency部分添加如下一行:

    implementation '[library_name]:[version]'
    

    例如,如果你想使用 1.3.0-alpha01 版本的片段库,添加这一行:

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

    如果您想查找每个库的名称,请查看:https://developer.android.com/jetpack/androidx/migrate/artifact-mappings

    要了解库的当前版本和最新更新:https://developer.android.com/jetpack/androidx/versions/

    【讨论】:

    • 可以直接在文件中添加片段库或者在设置中请告知。
    • @ShivamUpadhyay 我更新了我的答案。如果您仍需要帮助,请告诉我。
    猜你喜欢
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多