Android 引用样式属性 - 触发 ResourceNotFoundException

标签 android resources layout-inflater

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

 <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/

相关文章:

php - 是否有任何像样的 PHP 论坛可以让我提交程序设计和代码以供批评?

c# - 使用 C# 在 WPF 中更改背景图像

android - NullPointerException when inflating ,在图形设计器中

android - 我在gradle中使用firebase-ui时不会获得firebase token

java - 如何从 map 上删除符号

android - 如何防止 RecyclerView 在 notifyDataSetChanged 时求助

android - 以编程方式设置android :layout_centerHorizontal

ios - XCode (4.6.3) 在复制资源阶段调用 TiffUtil

android - 以编程方式为 Android 布局充气

java - Android 通过单击按钮删除动态创建的微调器和按钮