gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:19:35 25 4
gpt4 key购买 nike

我有一个 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/

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