0

項目を 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 を intilize しましたが、コードを実行すると 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)

リソースからビットマップを取得することに関係していると思いますが、修正方法がわかりません。

4

2 に答える 2

2

の参照として使用するため、Questionsはあなたのであると想定しています。ActivitygetResources()

その場合、コンストラクターに何かを入れて (おそらく初期化変数を除く)、それらが機能することを期待することはできません。の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
    }
于 2012-12-27T17:23:48.527 に答える
0

ここに NULL ポインタがあります: Questions.java:80

問題が何であるかを本当に知るには、このコードを含める必要があります。これは、StackTrace で報告されます (コードが参照されるまで、一番上から読み進めてください)。

于 2012-12-27T17:25:00.517 に答える