gpt4 book ai didi

android - Renderscript 分配崩溃

转载 作者:行者123 更新时间:2023-11-28 02:40:53 25 4
gpt4 key购买 nike

无法理解,因为当我尝试将 Allocation 用作输入和输出时,程序崩溃了。

我已经检查过是否由于 android list 中的某种类型的权限,但都没有。

project.properties如下:

target=android-19
android.library.reference.1=../appcompat_v7
renderscript.target=19
renderscript.support.mode=true
sdk.buildtools=20.0.0

代码如下:

...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

Allocation inputAlloc = Allocation.createFromBitmap(r, bmp, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

...

并且 logcat 向我显示以下错误:

09-25 11:32:08.623: E/AndroidRuntime(537): FATAL EXCEPTION: main
09-25 11:32:08.623: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpurenderscript/com.example.gpurenderscript.MainActivity}: java.lang.NullPointerException
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.os.Looper.loop(Looper.java:123)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-25 11:32:08.623: E/AndroidRuntime(537): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 11:32:08.623: E/AndroidRuntime(537): at java.lang.reflect.Method.invoke(Method.java:507)
09-25 11:32:08.623: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-25 11:32:08.623: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-25 11:32:08.623: E/AndroidRuntime(537): at dalvik.system.NativeStart.main(Native Method)
09-25 11:32:08.623: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
09-25 11:32:08.623: E/AndroidRuntime(537): at android.support.v8.renderscript.Allocation.createFromBitmap(Allocation.java:1337)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.support.v8.renderscript.Allocation.createFromBitmap(Allocation.java:1388)
09-25 11:32:08.623: E/AndroidRuntime(537): at com.example.gpurenderscript.MainActivity.onCreate(MainActivity.java:33)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-25 11:32:08.623: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-25 11:32:08.623: E/AndroidRuntime(537): ... 11 more

非常感谢!!

最佳答案

删除行:

options.inJustDecodeBounds = true

这是使用位图的大小信息设置 BitmapFactory,但 decodeResource() 方法实际上返回 null,因为没有为后备分配内存像素,所以没有 Bitmap 对象。您需要有后备像素才能实际创建 Allocation,以便它可以在 Java 和 RenderScript 之间编码数据。

关于android - Renderscript 分配崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035352/

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