0

クイズゲームを作成しようとしていますが、応答ボタンをクリックすると、が表示されますNullPointerException

すべての変数が初期化されているようですが、誰か助けてもらえますか?

これはクラスです:

public class QActivity extends Activity {

    protected int qst = 0;
    protected int err = 0;
    protected String mode = "";
    protected Intent intent;
    protected ArrayList<Integer> tb;
    protected Integer x = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_q);
        Button r1 = (Button) findViewById(R.id.rep1);
        Button r2 = (Button) findViewById(R.id.rep2);
        Button r3 = (Button) findViewById(R.id.rep3);
        TextView txt = (TextView) findViewById(R.id.questiontxt);
        Question q = new Question();
        DatabaseHelper myDbHelper = new DatabaseHelper(this);

        tb = getIntent().getExtras().getIntegerArrayList("tb");

        mode = getIntent().getExtras().getString("Mode");
        qst = getIntent().getExtras().getInt("qst");
        err = getIntent().getExtras().getInt("err");

        if (mode.equals("25")) {
            x = 0;

            do {
                x = (int) (Math.random() * 57);

            } while (Verif(x, tb) == true);

            tb.add(x);
            qst += 1;

            try {
                q = myDbHelper.getQuestion(x);
            } catch (SQLException sqle) {
                throw sqle;
            }

            txt.setText(q.getQuestion());
            r1.setText(q.getAnswer1());
            r2.setText(q.getAnswer2());
            r3.setText(q.getAnswer3());

            TextView t1 = (TextView) findViewById(R.id.questid);
            TextView t3 = (TextView) findViewById(R.id.questnumber);

            intent = new Intent(this, QActivity.class);
            intent.putExtra("Mode", mode);
            intent.putExtra("qst", qst);
            intent.putExtra("err", err);
            intent.putExtra("tab", tb);
        }

        r1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(intent);
            }
        });

    }

    public boolean Verif(int n, ArrayList<Integer> tbl) {
        for (Integer s : tbl) {
            if (s == n)
                return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_q, menu);
        return true;
    }

}

そしてこれはLogCatの出力です:

12-12 00:27:47.233: W/dalvikvm(7142): threadid=1: thread exiting with uncaught exception (group=0x40c471f8)
12-12 00:27:47.233: E/AndroidRuntime(7142): FATAL EXCEPTION: main
12-12 00:27:47.233: E/AndroidRuntime(7142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamappz.alamalmaarefa/com.dreamappz.alamalmaarefa.QActivity}: java.lang.NullPointerException
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.os.Looper.loop(Looper.java:137)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread.main(ActivityThread.java:4511)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at dalvik.system.NativeStart.main(Native Method)
12-12 00:27:47.233: E/AndroidRuntime(7142): Caused by: java.lang.NullPointerException
12-12 00:27:47.233: E/AndroidRuntime(7142):     at com.dreamappz.alamalmaarefa.QActivity.Verif(QActivity.java:87)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at com.dreamappz.alamalmaarefa.QActivity.onCreate(QActivity.java:47)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.Activity.performCreate(Activity.java:4470)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-12 00:27:47.233: E/AndroidRuntime(7142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
12-12 00:27:47.233: E/AndroidRuntime(7142):     ... 11 more
4

2 に答える 2

2

次の行を変更する必要があります。

intent.putExtra("tab", tb);

これに:

intent.putExtra("tb", tb);
于 2012-12-11T23:49:13.917 に答える
0

このclickListenerを使用することをお勧めします

r1.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (intent!=null)
            startActivity(intent);
    }
});

mode.equals( "25")がfalseの場合、インテントはnullであるためです。

于 2012-12-11T23:52:40.090 に答える