尝试模糊位图时出现android空指针异常

标签 android bitmap imageview android-custom-view blur

我有一个 ImageView,我想对其进行模糊处理。我正在使用这种方法进行模糊处理:https://futurestud.io/tutorials/how-to-blur-images-efficiently-with-androids-renderscript

这是初始化 ImageView 的布局 XML 文件的一部分:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/InfoConstraint"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="0.9"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:color/background_light" /> 

此约束布局也在另一个约束布局内。然后,我在带有 Id 的 custom view 中定位 backdrop 并提取 bitmap ,这样我就可以使用上面链接中的代码来模糊位图。这样做之后,我将 bitmap 转换回 ImageView

backdrop = findViewById(R.id.backdrop);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.id.backdrop);
Bitmap blurredBitmap = BlurBuilder.blur(context, b);
backdrop.setImageBitmap(blurredBitmap);

唯一的问题是当我调用 BlurBuilder 时出现空指针异常。这是错误堆栈:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.example.sapp.BlurBuilder.blur(BlurBuilder.java:15)
        at views.customsapp$override.init(customsapp.java:73)
        at views.customsapp$override.access$dispatch(Unknown Source:67)
        at views.customsapp.init(Unknown Source:15)
        at views.customsapp$override.init$body(customsapp.java:48)
        at views.customsapp$override.access$dispatch(Unknown Source:132)
        at views.customsapp.<init>(customsapp.java:46)
            ... 33 more

它似乎没有找到 backdrop 这里有什么问题?我想不明白。我敢打赌,当我从 ImageView 创建位图时发生了一些我不知道的事情,因为背景显然存在,因为我正在调用有效的 findViewById()

更新

这是我尝试 Anjana 的回答时抛出的新异常

Caused by: java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1113)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1080)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1030)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:991)
E/AndroidRuntime:     at views.customsapp

.init(customsapp.java:78)
    at views.customsapp.<init>(customsapp.java:48)
        ... 28 more

最佳答案

它可能会找到 backdrop。问题就在这里 Bitmap b = BitmapFactory.decodeResource(getResources(), R.id.backdrop);

您向 decodeResource 传递了错误的内容。你应该传递一个 drawable(R.drawable.bla_bla)。 看这个question

关于尝试模糊位图时出现android空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221255/

相关文章:

android - Ionic项目的发布配置,类似于Android studio中的flavors

android - Gradle 版本 4.6 - 设置输出文件名时不支持绝对路径

android - Android 模拟器上不显示 Tifinagh 字符

android - 创建一个新位图并在其中绘制新像素

android - 使用来自 MySQL 的数据以编程方式设置 ImageView 的高度

android - undefined reference 错误使用静态库构建共享库

android - 在 NDK 中处理 RGB_565 位图

java - 加载照片时位图 OutOfMemoryException

android - ImageView 中带圆角的位图

android - 如何创建此 View (布局)?