我想创建自定义复合视图。该视图在运行时运行良好,但无法在布局预览中绘制自身。为了研究这个问题,我创建了简单的复合视图,下面是示例:
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)
项目是多模块的,并且视图在库模块中声明。此库模块由应用程序中的所有其他模块使用。
常数0x7F060034在每个模块的R文件中显示为非最终常量,并在应用程序的R文件中显示为最终0x7f060044。
我也尝试在init()方法中给一些布局文件充气,并且此布局资源也无法解析。我试图清除,重建,使缓存无效并重新导入项目。在其他计算机上也会重复出现此问题。
Android Studio 3.2,gradle-4.9
我认为android studio布局预览中的资源链接器中有错误,或者我以某种方式错误地配置了我的项目。我在应用程序模块中创建了此自定义视图,并且在布局预览中显示良好。
答案 0 :(得分:0)
您可以通过更改预览的设备和预览的API版本来尝试。如果对您不起作用。您可以遵循此Android layout previewer throws error when adding custom LinearLayout的答案。
基本上,您需要清除缓存并重新启动IDE才能解决