gpt4 book ai didi

Android 引用样式属性 - 触发 ResourceNotFoundException

转载 作者:行者123 更新时间:2023-11-30 04:08:30 26 4
gpt4 key购买 nike

我这样创建自己的样式属性:

 <attr name="color_foreground" format="color|reference" />

我在我的主题中赋予它这样的值(value):

    <item name="color_foreground">@color/blue</item>

如果我在我的布局中访问它(已设置为 contentView)

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?color_foreground"
>

它就像一个魅力。但是如果我在膨胀的布局中做同样的事情,我会收到一个错误:

        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myInflatedLayout, null);

错误输出:

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010005 a=-1}
06-27 20:33:37.340: E/AndroidRuntime(31616): at android.content.res.Resources.loadDrawable(Resources.java:1899)
06-27 20:33:37.340: E/AndroidRuntime(31616): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-27 20:33:37.340: E/AndroidRuntime(31616): at android.view.View.<init>(View.java:2810)
06-27 20:33:37.340: E/AndroidRuntime(31616): at android.widget.TextView.<init>(TextView.java:561)
06-27 20:33:37.340: E/AndroidRuntime(31616): at android.widget.TextView.<init>(TextView.java:554)
06-27 20:33:37.340: E/AndroidRuntime(31616): ... 48 more

二进制 XML 第 15 行实际上是我通过“?color_foreground”访问属性的行

如果我尝试获取这样的属性(在设置内容 View 之后),几乎会发生同样的情况:

this.getResources().getColor(R.attr.color_foreground);

错误:

06-28 12:55:16.975: E/AndroidRuntime(7089): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f010005
06-28 12:55:16.975: E/AndroidRuntime(7089): at android.content.res.Resources.getValue(Resources.java:1019)
06-28 12:55:16.975: E/AndroidRuntime(7089): at android.content.res.Resources.getColor(Resources.java:749)
06-28 12:55:16.975: E/AndroidRuntime(7089): at ***.MyActivity.onCreate(AppointmentListActivity.java:66)
06-28 12:55:16.975: E/AndroidRuntime(7089): at android.app.Activity.performCreate(Activity.java:4465)
06-28 12:55:16.975: E/AndroidRuntime(7089): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
06-28 12:55:16.975: E/AndroidRuntime(7089): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
06-28 12:55:16.975: E/AndroidRuntime(7089): ... 11 more

最佳答案

问题是,我在 Activity 的背景下工作。如果我在 Activity 本身上工作,它不会造成任何问题。

关于Android 引用样式属性 - 触发 ResourceNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com