簡體   English   中英

未顯示 Android Studio 預覽

[英]Android studio preview not shown

無法實例化以下類: 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 我解決了這個問題,然后顯示了上面的消息。 我嘗試過已知的解決方案,例如重建、刷新布局和使緩存/重啟無效等,但他們沒有成功。

如果您查看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) {
    // ...
}

如果你實際運行你的應用程序,這應該被調用,一切都應該正常工作。

今天遇到了同樣的問題。 看看這個: https : //developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha01

他們已經注意到這個問題並發布了一個更新的版本來修復這個錯誤。

我嘗試使用這個版本的 Fragment 庫,結果不再有錯誤,但仍然沒有在布局預覽中顯示這些片段。 不確定這是我的錯誤還是他們仍在努力。

更新

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

打開文件build.gradle (Module: app) ,在依賴項部分添加以下行:

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM