1

我有一个带有以下代码的片段:

final MyActivity activity = (MyActivity )getActivity();

然后稍后:

Drawable d = (Drawable) ((Context)(activity)).getResources().getDrawable(R.drawable.marker_report_path_merge);
Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
LayerDrawable ld = (LayerDrawable) ((Context)(activity)).getResources().getDrawable(R.drawable.marker_report_path_merge);
ld.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ld.draw(new Canvas(b));

我收到此错误:

07-31 19:59:12.491: E/AndroidRuntime(14341): FATAL EXCEPTION: main
07-31 19:59:12.491: E/AndroidRuntime(14341): android.content.res.Resources$NotFoundException: File res/drawable-hdpi/marker_report_path_merge.xml from drawable resource ID #0x7f020049
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.content.res.Resources.loadDrawable(Resources.java:1951)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.content.res.Resources.getDrawable(Resources.java:672)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at com.recntrek.util.FragmentReportUtils$2.onClick(FragmentReportUtils.java:56)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.view.View.performClick(View.java:4211)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.view.View$PerformClick.run(View.java:17267)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.os.Handler.handleCallback(Handler.java:615)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.os.Looper.loop(Looper.java:137)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at dalvik.system.NativeStart.main(Native Method)
07-31 19:59:12.491: E/AndroidRuntime(14341): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: <bitmap> requires a valid src attribute
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:448)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:165)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
07-31 19:59:12.491: E/AndroidRuntime(14341):    at android.content.res.Resources.loadDrawable(Resources.java:1948)
07-31 19:59:12.491: E/AndroidRuntime(14341):    ... 13 more

但我所有的资源都存在!

我尝试在 Eclipse 中清理构建,但没有奏效。

谁能帮我?

谢谢

编辑:

这是 marker_report_path_merge.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/marker_report_blue" />
    <item>
      <bitmap android:src="@drawable/report_path_merge"
              android:gravity="center_vertical|center_horizontal" />
    </item>
</layer-list>
4

1 回答 1

1

The reason why you had this error was because you didn't have a real bitmap in your <item> tag it layer-list. You can check this code in BitmapDrawable class, inflate method

final Bitmap bitmap = BitmapFactory.decodeResource(r, id);
if (bitmap == null) {
    throw new XmlPullParserException(parser.getPositionDescription() +
            ": <bitmap> requires a valid src attribute");
}

You posted exactly this exception.

于 2013-07-31T17:35:09.297 回答