Renderscript分配崩溃

时间:2014-09-25 09:43:06

标签: android c++ renderscript

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

我已经检查过是否由于某些类型的安装权限,但两者都没有。

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

非常感谢!!

1 个答案:

答案 0 :(得分:2)

删除该行:

options.inJustDecodeBounds = true

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