资源$ ResourcesNotFound:资源不是可绘制的

时间:2013-02-20 19:00:40

标签: android android-layout xml-parsing

我正在努力找到解决此错误的方法:

02-20 19:50:36.027: E/AndroidRuntime(26842): FATAL EXCEPTION: main
02-20 19:50:36.027: E/AndroidRuntime(26842): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.createView(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.inflate(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.inflate(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.LayoutInflater.inflate(LayoutInflater.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at Utility.AdapterListaLinee.getView(AdapterListaLinee.java:59)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.AbsListView.obtainView(AbsListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.ListView.makeAndAddView(ListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.ListView.fillDown(ListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.ListView.fillFromTop(ListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.ListView.layoutChildren(ListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.AbsListView.onLayout(AbsListView.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.layout(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.layout(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.Choreographer.doCallbacks(Choreographer.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.Choreographer.doFrame(Choreographer.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.os.Handler.handleCallback(Handler.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.os.Handler.dispatchMessage(Handler.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.os.Looper.loop(Looper.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.app.ActivityThread.main(ActivityThread.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at java.lang.reflect.Method.invoke(Method.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at dalvik.system.NativeStart.main(Native Method)
02-20 19:50:36.027: E/AndroidRuntime(26842): Caused by: java.lang.reflect.InvocationTargetException
02-20 19:50:36.027: E/AndroidRuntime(26842):    at java.lang.reflect.Constructor.constructNative(Native Method)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at java.lang.reflect.Constructor.newInstance(Constructor.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    ... 51 more
02-20 19:50:36.027: E/AndroidRuntime(26842): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010070 a=-1}
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.content.res.Resources.loadDrawable(Resources.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.content.res.TypedArray.getDrawable(TypedArray.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.<init>(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.View.<init>(View.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.view.ViewGroup.<init>(ViewGroup.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    at android.widget.RelativeLayout.<init>(RelativeLayout.java)
02-20 19:50:36.027: E/AndroidRuntime(26842):    ... 53 more

来自styles.xml的一些重要代码:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:actionBarStyle">@style/ActionBar</item>
        <item name="ColSfondo">#00000000</item>
        <item name="ColSfondoVociListe">@drawable/SfondoVociListe</item>
        <item name="ColSfondoVoceInfotraffico">#00000000</item>
        <item name="ColTestoFields">#ff000000</item>
        <item name="ColTestoVociListe">@drawable/ColoreTestoVoci</item>
    </style>

    <style name="AppThemeDark" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:actionBarStyle">@style/ActionBar</item>
        <item name="ColSfondo">@drawable/holo_background_gradient</item>
        <item name="ColSfondoVociListe">#00000000</item>
        <item name="ColSfondoVoceInfotraffico">@drawable/holo_background_gradient</item>
        <item name="ColTestoFields">#ffffffff</item>
        <item name="ColTestoVociListe">@drawable/ColoreTestoVoci</item>
    </style>

    <attr name="ColSfondo" format="color|reference" />
    <attr name="ColSfondoVociListe" format="color|reference" />
    <attr name="ColSfondoVoceInfotraffico" format="color|reference" />
    <attr name="ColTestoFields" format="color|reference" />
    <attr name="ColTestoVociListe" format="color|reference" />

    <drawable name="ColoreTestoVoci">#ffffffff</drawable>
    <drawable name="SfondoVociListe">#dd212121</drawable>
    <color name="ColoreSecondarioVoci">#ffdddddd</color>

 [...]

</resources>

以下是导致问题的xlm布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/ColSfondoVociListe" >

    <ImageView
        android:id="@+id/ListaLineeImage"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_lista" />

    <TextView
        android:id="@+id/ListaLineeTitolo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/ListaLineeImage"
        android:gravity="center_vertical"
        android:text="Medium Text"
        android:textColor="?ColTestoVociListe"
        android:textSize="@dimen/Titolo" />

</RelativeLayout>

如果我删除“?ColTestoVociListe”和“?attr / ColSfondoVociListe”,用#aarrggbb这样的颜色替换它就可以了。 但我需要使用属性,与主题一起改变颜色。在其他xml文件中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/ColSfondoVociListe" >

  [.....]

</RelativeLayout>

没有错误!为什么????相同的属性,拼写完全相同的方式.. 我已经多次尝试删除并重新创建R.java并清理项目。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

有同样的问题here

为了制作您的主题,您需要:

为每个主题创建一个xml drawable。使用@color标签或#RGB格式直接包含所需的颜色。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <stroke android:width="1dp" android:color="#123456" />
</shape>

在attrs.xml中为你的drawable提供一个属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <!-- attributes mut be low case as we want to use them for drawables -->
   <attr name="my_drawable" format="reference" />
</resources>

将您的drawable添加到theme.xml。

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
   <item name="my_drawable">@drawable/my_drawable</item>
</style>

使用您的属性在布局中引用您的drawable。

<TextView android:background="?my_drawable" />