簡體   English   中英

Android Studio無法在布局預覽中解析自定義視圖的資源

[英]Android Studio cannot resolve resources for custom views in layout preview

我想創建自定義復合視圖。 該視圖在運行時運行良好,但無法在布局預覽中繪制自身。 為了研究這個問題,我創建了簡單的復合視圖,下面是示例:

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import my.testproject.coreui.R;

public class ImageWithCaption3 extends LinearLayout {

    private TextView textView;

    public ImageWithCaption3(Context context) {
        this(context, null);
    }

    public ImageWithCaption3(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    public ImageWithCaption3(
            Context context,
            AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);

        textView = new TextView(getContext());
        LayoutParams layoutParams = new LayoutParams(
                128,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        textView.setLayoutParams(layoutParams);
        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.core_ui_gray));
        textView.setText("Test text");

        addView(textView);
    }
}

堆棧跟蹤:

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F060034.
at android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1117)
at android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1093)
at android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1097)
at android.content.res.Resources_Delegate.getColorStateList(Resources_Delegate.java:258)
at android.content.res.Resources_Delegate.getColor(Resources_Delegate.java:236)
at android.content.res.Resources.getColor(Resources.java:958)
at android.content.Context.getColor(Context.java:610)
at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:523)
at my.testproject.coreui.widgets.ImageWithCaption3.init(ImageWithCaption3.java:45)
at my.testproject.coreui.widgets.ImageWithCaption3.<init>(ImageWithCaption3.java:32)
at my.testproject.coreui.widgets.ImageWithCaption3.<init>(ImageWithCaption3.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:401)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:184)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:142)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:229)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:421)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:432)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:336)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:327)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:386)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:450)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:590)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

項目是多模塊的,並且視圖在庫模塊中聲明。 此庫模塊由應用程序中的所有其他模塊使用。

常數0x7F060034在每個模塊的R文件中顯示為非最終常數,並在應用程序的R文件中顯示為最終0x7f060044。

我也嘗試在init()方法中給一些布局文件充氣,並且此布局資源也無法解析。 我試圖清除,重建,使緩存無效並重新導入項目。 在其他計算機上也會重復出現此問題。

Android Studio 3.2,Gradle-4.9

我認為android studio布局預覽中的資源鏈接器中有一個錯誤,或者我以某種方式錯誤地配置了我的項目。 我在應用程序模塊中創建了此自定義視圖,並且在布局預覽中顯示良好。

您可以通過更改預覽的設備和預覽的API版本來嘗試。 如果對您不起作用。 添加自定義LinearLayout時,您可以遵循此Android布局預覽器引發錯誤的答案。

基本上,您需要清除緩存並重新啟動IDE才能解決它

暫無
暫無

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

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