将可绘制资源转换为Bitmap Android

时间:2014-11-25 16:19:39

标签: android xml bitmap drawable android-resources

我正在构建一个Npuzzle应用程序 我面临的当前问题是将一个可绘制对象带入我的Activity中,以便我可以将其转换为位图以进行拆分。我发现并试图使用这种方法:

Drawable hold = Puzzle.this.getResources().getDrawable(R.drawable.img1);
BitmapDrawable hold2 = (BitmapDrawable) hold;
Bitmap bitPhoto = hold2.getBitmap();

当我运行我的应用程序时,我在第一行代码中得到一个空指针异常。有关如何解决此问题的任何建议?这是我运行程序时的logcat:

 11-25 12:07:46.441    5388-5388/com.example.anthony.puzzler E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.anthony.puzzler/com.example.anthony.puzzler.Puzzle}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
        at com.example.anthony.puzzler.Puzzle.<init>(Puzzle.java:28)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

为什么不直接将媒体作为位图?

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.img1);

应该比获得一个drawable并将其投射更简单。

答案 1 :(得分:1)

清理项目并重建它以确保drawable的id是正确的。

检查getResources()是否实际返回了某些内容。

编辑:在super.onCreate()方法返回之前调用getResources(),它会导致NPE。