gpt4 book ai didi

来自 Fragment 内部的 Android getDrawable()

转载 作者:行者123 更新时间:2023-11-30 03:27:40 28 4
gpt4 key购买 nike

我有一个包含以下代码的 fragment :

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>

最佳答案

您出现此错误的原因是因为您的 <item> 中没有真正的位图。标记它层列表。您可以在 BitmapDrawable 类、inflate 方法中检查此代码

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

您发布的正是这个异常。

关于来自 Fragment 内部的 Android getDrawable(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976495/

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