2
我正試圖在畫布上繪製位圖。位圖返回null,但我不明白爲什麼。我已經閱讀了Bitmap和decodeResources的文檔。它看起來像drawable返回null,因爲解碼無法找到它。我試圖使用調試會話,但我不是很遠。在Draw方法中位圖的NullPointer
請指出空例外。
故障類別:
public class SnapShot extends Activity {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public void draw(Canvas canvas) {
drawIcon(canvas);
}
private void drawIcon(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.buttongreen); //this is line 36
canvas.drawBitmap(bitmap, 150, 150, paint);
}
}
堆棧跟蹤:
10-01 09:24:30.638: E/AndroidRuntime(27206): FATAL EXCEPTION: main
10-01 09:24:30.638: E/AndroidRuntime(27206): java.lang.NullPointerException
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.game.SnapShot.drawIcon(SnapShot.java:36)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.game.SnapShot.draw(SnapShot.java:29)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.game.onDraw(AugmentedView.java:77)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.View.draw(View.java:6880)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.View.draw(View.java:6883)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.widget.FrameLayout.draw(FrameLayout.java:357)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.View.draw(View.java:6883)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.widget.FrameLayout.draw(FrameLayout.java:357)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewRoot.draw(ViewRoot.java:1522)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.os.Looper.loop(Looper.java:130)
10-01 09:24:30.638: E/AndroidRuntime(27206): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-01 09:24:30.638: E/AndroidRuntime(27206): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 09:24:30.638: E/AndroidRuntime(27206): at java.lang.reflect.Method.invoke(Method.java:507)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-01 09:24:30.638: E/AndroidRuntime(27206): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-01 09:24:30.638: E/AndroidRuntime(27206): at dalvik.system.NativeStart.main(Native Method)
需要更多信息,也許設定線36斷點,看空的對象是什麼。 – iCantSeeSharp
@Souvlaki使用調試器告訴我ViewRoot.draw(boolean)返回null。從閱讀decodeResource如何工作如果無法找到位圖的int ID它將返回null。然而使用R.drawable格式應該返回id的權利?它編譯,所以我會假設它可以在該方法中找到可繪製的罰款.. – KDEx