0

この1行でアプリがクラッシュするのはなぜですか?それは、クリック可能なセットでも起こります。線がなければ、それは問題ありませんが、それがあれば、それは活動にさえ入りません。

continueButton.setEnabled(false);

これがlogcatです。

09-30 16:05:09.352: E/AndroidRuntime(755): FATAL EXCEPTION: main
09-30 16:05:09.352: E/AndroidRuntime(755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mangodeveloper.mcathomie/com.mangodeveloper.mcathomie.McatActivityGame}: java.lang.NullPointerException
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.os.Looper.loop(Looper.java:123)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 16:05:09.352: E/AndroidRuntime(755):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:05:09.352: E/AndroidRuntime(755):  at java.lang.reflect.Method.invoke(Method.java:507)
09-30 16:05:09.352: E/AndroidRuntime(755):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 16:05:09.352: E/AndroidRuntime(755):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 16:05:09.352: E/AndroidRuntime(755):  at dalvik.system.NativeStart.main(Native Method)
09-30 16:05:09.352: E/AndroidRuntime(755): Caused by: java.lang.NullPointerException
09-30 16:05:09.352: E/AndroidRuntime(755):  at com.mangodeveloper.mcathomie.McatActivityGame.onCreate(McatActivityGame.java:40)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 16:05:09.352: E/AndroidRuntime(755):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-30 16:05:09.352: E/AndroidRuntime(755):  ... 11 more

これが今のところ全体の活動です:

package com.mangodeveloper.mcathomie;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class McatActivityGame extends Activity {

    private static int GAME_PREFERENCES_MAXROUNDS = 1, GAME_PREFERENCES_CURRENTROUND;
    private Cursor c;
    private RadioGroup radioGrp;
    private Button continueButton, pauseButton, explainButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        radioGrp = (RadioGroup) findViewById(R.id.radioGroup1);
        continueButton = (Button) findViewById(R.id.button_continue); 
        pauseButton = (Button) findViewById(R.id.button_pause); 
        explainButton = (Button)findViewById(R.id.button_explanation);  

        setContentView(R.layout.activity_game);
        McatDatabase dbHelper = new McatDatabase(this);
        c = dbHelper.getQuestions();

        c.moveToFirst();
        fillQuestions();
        continueButton.setEnabled(false);

//      radioGrp.setOnClickListener(new OnClickListener() {
//          
//          public void onClick(View v) {
//              continueButton.setEnabled(true);
//          }
//      });

//      continueButton.setOnClickListener(new OnClickListener() {
//          public void onClick(View v) {
//              int selectedId = radioGrp.getCheckedRadioButtonId();
//              RadioButton radioBttnSelected = (RadioButton) findViewById(selectedId);
//              if (radioBttnSelected.getText() == answer1) {
//                  Toast.makeText(McatActivityGame.this, "Correct Answer!", Toast.LENGTH_SHORT).show();
//              }else{Toast.makeText(McatActivityGame.this, "Not correct, please try again", Toast.LENGTH_SHORT).show();
//              }
//          }
//      });



//      nextquestionButton.setOnClickListener(new OnClickListener() {
//
//          public void onClick(View v) {
//              GAME_PREFERENCES_CURRENTROUND++;
//              GAME_PREFERENCES_MAXROUNDS = getIntent().getExtras().getInt("MAXROUNDS");
//
//              if (GAME_PREFERENCES_CURRENTROUND > GAME_PREFERENCES_MAXROUNDS) {
//                  Toast.makeText(McatActivityGame.this, "Alert Dialog stuff", Toast.LENGTH_SHORT).show();
//              }else{
//                  c.moveToNext(); 
//                  fillQuestions();
//              }
//          }
//      });     
//      

    }

    private void fillQuestions() {  
//      shuffle questions
        List<String> shuffAnswr = new ArrayList<String>(4);
        shuffAnswr.add(c.getString(3));
        shuffAnswr.add(c.getString(4));
        shuffAnswr.add(c.getString(5));
        shuffAnswr.add(c.getString(6));
        Collections.shuffle(shuffAnswr);        

//      instantiate 
        TextView questionTv = (TextView) findViewById(R.id.textView0);  
        RadioButton rd0 = (RadioButton)findViewById(R.id.radio0),
                rd1 = (RadioButton)findViewById(R.id.radio1),
                rd2 = (RadioButton)findViewById(R.id.radio2),
                rd3 = (RadioButton)findViewById(R.id.radio3);

//      set questions
        questionTv.setText(c.getString(2));
        rd0.setText(shuffAnswr.get(0));
        rd1.setText(shuffAnswr.get(1));
        rd2.setText(shuffAnswr.get(2));
        rd3.setText(shuffAnswr.get(3));
    }

}
4

1 に答える 1

1

onCreateを次のように変更します。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);//This got moved up

        radioGrp = (RadioGroup) findViewById(R.id.radioGroup1);
        continueButton = (Button) findViewById(R.id.button_continue); 
        pauseButton = (Button) findViewById(R.id.button_pause); 
        explainButton = (Button)findViewById(R.id.button_explanation);  

        McatDatabase dbHelper = new McatDatabase(this);
        c = dbHelper.getQuestions();

        c.moveToFirst();
        fillQuestions();
        continueButton.setEnabled(false);
        ....... //Rest of your code

レイアウトが拡張される前に、レイアウト内のビューへの参照を取得しようとしたため、エラーが発生していました。setContentView()を使用する前に、必ず電話をかける必要がありますfindViewById()

于 2012-09-30T16:18:51.887 に答える