獲取位圖我有這樣的代碼將項目添加到一個ArrayList:從資源
public Questions(int questionMode) {
getID = new Random();
allQuestions = new ArrayList<Question>();
allQuestions.add(new Question(2, 0, 0, 2, 5, (BitmapFactory.decodeResource(getResources(), R.drawable.flag_dutch)), "The Netherlands", true)); //<-- error at this line
}
我在類的構造函數intilize allQuestions,但是當我運行的代碼,我得到一個NullPointerException。這是我的logcat:
12-27 18:14:25.335: E/InputEventReceiver(3583): Exception dispatching input event.
12-27 18:14:25.335: E/MessageQueue-JNI(3583): Exception in MessageQueue callback: handleReceiveCallback
12-27 18:14:25.340: E/MessageQueue-JNI(3583): java.lang.NullPointerException
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.obattech.twoplayergame.Questions.createFlagQuestions(Questions.java:80)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.obattech.twoplayergame.Questions.<init>(Questions.java:54)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.obattech.twoplayergame.games.GuessFlag.startCountDown(GuessFlag.java:199)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.obattech.twoplayergame.games.GuessFlag$1.onTouch(GuessFlag.java:75)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.View.dispatchTouchEvent(View.java:7332)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.View.dispatchPointerEvent(View.java:7520)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.os.MessageQueue.nativePollOnce(Native Method)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.os.MessageQueue.next(MessageQueue.java:125)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.os.Looper.loop(Looper.java:124)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-27 18:14:25.340: E/MessageQueue-JNI(3583): at dalvik.system.NativeStart.main(Native Method)
12-27 18:14:25.340: D/AndroidRuntime(3583): Shutting down VM
12-27 18:14:25.340: W/dalvikvm(3583): threadid=1: thread exiting with uncaught exception (group=0x410e72a0)
12-27 18:14:25.350: E/AndroidRuntime(3583): FATAL EXCEPTION: main
12-27 18:14:25.350: E/AndroidRuntime(3583): java.lang.NullPointerException
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.obattech.twoplayergame.Questions.createFlagQuestions(Questions.java:80)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.obattech.twoplayergame.Questions.<init>(Questions.java:54)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.obattech.twoplayergame.games.GuessFlag.startCountDown(GuessFlag.java:199)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.obattech.twoplayergame.games.GuessFlag$1.onTouch(GuessFlag.java:75)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.View.dispatchTouchEvent(View.java:7332)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2117)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.View.dispatchPointerEvent(View.java:7520)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.os.MessageQueue.nativePollOnce(Native Method)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.os.MessageQueue.next(MessageQueue.java:125)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.os.Looper.loop(Looper.java:124)
12-27 18:14:25.350: E/AndroidRuntime(3583): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-27 18:14:25.350: E/AndroidRuntime(3583): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 18:14:25.350: E/AndroidRuntime(3583): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-27 18:14:25.350: E/AndroidRuntime(3583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-27 18:14:25.350: E/AndroidRuntime(3583): at dalvik.system.NativeStart.main(Native Method)
我認爲這是與從資源獲得一個位圖,但我無法弄清楚如何解決它。
OP知道這一點,並且對導致問題的行(即第80行)發出了'// //註釋。我認爲這個答案更適合作爲評論... – Eric