java.lang.ClassCastException : android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable

标签 java android classcastexception

我正在尝试通过具有以下规范的设备来解决 Android 应用程序崩溃的问题:

Device
PAP3400DUO 1
Manufacturer — Android version Android 4.2 RAM (MB) — Screen size 480 × 800 Screen density (dpi) 240 OpenGL ES version 2.0 Native platform armeabi-v7a

错误:

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
    at com.yitter.android.activity.EditProfileActivity.lambda$setSubmitProfileChangesClickListener$46(EditProfileActivity.java:120)
    at com.yitter.android.activity.EditProfileActivity.access$lambda$1(EditProfileActivity.java)
    at com.yitter.android.activity.EditProfileActivity$$Lambda$2.onClick(Unknown Source)
    at android.view.View.performClick(View.java:4212)
    at android.view.View$PerformClick.run(View.java:17476)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

它真的指向我第 120 行吗?那里的代码大致显示了这一点:

ImageView imageView = (ImageView) findViewById(R.id.profile_picture);
            Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] image = stream.toByteArray();
            ParseFile file = new ParseFile("profilePicture.png", image);
            user1.put("profilePicture", file);

问题可能是什么?我研究了抛出的 ClassCastException 但我似乎无法将两者放在一起。 drawable.ColorDrawable 与我的位图有什么关系?

编辑:

<ImageView
                        android:id="@+id/profile_picture"
                        android:layout_width="87dp"
                        android:layout_height="87dp"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:clickable="true"
                        android:contentDescription="@string/content_desc_profile_picture"
                        android:scaleType="fitXY"
                        android:src="@android:color/transparent" />

最佳答案

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

显然,在发生此异常的情况下,imageView 不包含 BitmapDrawable,而是包含 ColorDrawable。您需要检查填充此 ImageView 的所有不同位置。或者,更好的是,不要尝试从 ImageView 中取出可绘制对象,而是从模型对象或用于填充 ImageView 的任何数据源获取图像。首先。

关于java.lang.ClassCastException : android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742357/

相关文章:

java - REST GET 列表为 JSON

java - 通过 jUnit 的 BDD Cucumber 测试

java - 创建一个新的颜色drawable

android - 无法在android中使用Asynchttpclient将图像上传到服务器

Solr DataImportHandler CachedSqlEntityProcessor ClassCastException

java - 在我的 Java 应用程序中发现 ClassCastException

java - web.xml 中的元素顺序重要吗?

java - 重复循环大量对象时如何优化性能

android - 将项目添加到操作栏(使用 ActionBarSherlock)

Java Generics GetThis Trick 解释