【问题标题】:Android Studio cannot resolve resources for custom views in layout previewAndroid Studio 无法解析布局预览中自定义视图的资源
【发布时间】:2018-11-04 07:10:01
【问题描述】:

我想创建自定义复合视图。此视图在运行时工作正常,但无法在布局预览中绘制自身。为了调查这个问题,我创建了简单的复合视图,下面是示例:

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)

项目是多模块的,视图在库模块中声明。这个库模块被 app 中的所有其他模块使用。

常量 0x7F060034 作为非最终常量出现在每个模块的 R 文件中,并在应用程序的 R 文件中作为最终 0x7f060044。

我还尝试在 init() 方法中填充一些布局文件,但该布局资源也无法解析。我试图清除、重建、使缓存无效并重新导入项目。此问题在其他计算机上也会重复。

Android Studio 3.2,gradle-4.9

我认为 android studio 布局预览中的资源链接器存在错误,或者我以某种方式错误配置了我的项目。我在应用程序模块中创建了这个自定义视图,它在布局预览中显示良好。

【问题讨论】:

  • core_ui_gray 存在吗?
  • 您是否尝试更改预览的设备和预览的 API 版本
  • 是的,core_ui_gray 颜色存在,因为它在运行时工作正常
  • 是的,我尝试在布局预览中更改设备和 api 版本,但没有帮助

标签: java android android-studio


【解决方案1】:

您可以通过更改预览的设备和预览的API版本来尝试。如果它不适合你。您可以关注此Android layout previewer throws error when adding custom LinearLayout 的答案。

基本上,你需要清除缓存并重新启动你的IDE来解决它

【讨论】:

  • 试图清除、重建、无效\重启 - 它没有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 2013-05-11
  • 2018-03-15
相关资源
最近更新 更多