2014-09-25 91 views
0

無法理解,因爲當我嘗試使用分配作爲輸入和輸出時,程序崩潰。Renderscript分配崩潰

我檢查了是否由於Android清單中的某種類型的權限,但都沒有。

的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 

非常感謝!

回答

2

刪除行:

options.inJustDecodeBounds = true

這是建立BitmapFactory與定型信息爲您的位圖,但decodeResource()方法實際上是返回null,因爲沒有分配爲後盾像素內存,所以沒有Bitmap的對象。爲了使Allocation真正被創建,您需要有後備像素,以便它可以編組Java和RenderScript之間的數據。