1

I want to create custom compound view. This view works fine in runtime, but fails to draw itself in layout preview. To investigate this problem, i created simple compound view, here's example:

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);
    }
}

Stacktrace:

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)

Project is multimodule, and view declared in library module. This library module used by every other module in app.

Constant 0x7F060034 appears in every module's R file as non-final constant, and as final 0x7f060044 in application's R file.

Also i tried to inflate some layout file inside init() method, and this layout resource also failed to resolve. I tried to clear, rebuild, invalidate caches, and reimport project. This issue repeats on other computers as well.

Android studio 3.2, gradle-4.9

I think there's a bug in resource linker in android studio layout preview, or i somehow misconfigured my project. I created this custom view in application module, and it displays fine in layout preview.

1 Answers1

-1

You can try it by change device of preview and API version of the preview. If it is not work for u. You can follow answers of this Android layout previewer throws error when adding custom LinearLayout .

Basically, you need to clear cache and restart your IDE to resolve it

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23