我有这个代码将项添加到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
}
我在类的构造函数中使用了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)
我认为它与从资源中获取Bitmap有关,但我无法弄清楚如何修复它。
答案 0 :(得分:2)
我假设Questions
是您的Activity
,因为您将其用作getResources()
的参考。
既然如此,你不能把东西放在构造函数中(除了可能是初始化变量)并期望它们能够工作; Activity
的资源尚未准备好在此时使用。请改用onCreate()
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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
}
答案 1 :(得分:0)
这里有一个空指针:Questions.java:80
您需要包含此代码才能真正了解问题所在。这在StackTrace中报告(只是从顶部开始,然后读下来,直到你看到你的代码被引用)。