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

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

我正在尝试从具有以下规格的设备上分解我的 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(EditProfileActivity.java:120)
    at com.yitter.android.activity.EditProfileActivity.access$lambda(EditProfileActivity.java)
    at com.yitter.android.activity.EditProfileActivity$$Lambda.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 的任何数据源中获取图像。