-2

このアクティビティを開始しようとしても、実行されません。

LogCat には次のエラーが表示されます。

07-25 12:10:46.813: E/AndroidRuntime(797): FATAL EXCEPTION: main
07-25 12:10:46.813: E/AndroidRuntime(797): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testingandroid/com.example.testingandroid.DoSums}: java.lang.NullPointerException

私のコード:

public class DoSums extends Activity implements OnClickListener {
    public RadioButton r1 = (RadioButton) findViewById(R.id.doradioButton1);
    public RadioButton r2 = (RadioButton) findViewById(R.id.doradioButton2);
    public RadioButton r3 = (RadioButton) findViewById(R.id.doradioButton3);
    public RadioButton r4 = (RadioButton) findViewById(R.id.doradioButton4);

    public EditText et = (EditText) findViewById(R.id.doeditText1);
    //Button btnNextScreen = (Button) findViewById(R.id.button1);

    timeanddistanceM pen = new timeanddistanceM();

    public String Q, A, finalvalue, scale;
    public double fv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_sums);

        objCreator();
        //btnNextScreen.setOnClickListener(this);
        r1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r3.setOnClickListener(this);
        r4.setOnClickListener(this);
    }

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

    public void objCreator() {

        pen.timeanddistanceM();
        // Setting question
        String Q = pen.Q;
        et.setText("Question" + Q);
        // Setting options

        // 1. Getting options
        options ops = new options();
        ops.optionsCreator(pen.fv, pen.scale);

        // 2.Assigning options
        r1.setText(ops.jRadioButton1);
        r2.setText(ops.jRadioButton2);
        r3.setText(ops.jRadioButton3);
        r4.setText(ops.jRadioButton4);
    }

    public void RadiobuttonAction(RadioButton testRadio) {

        if (testRadio.getText().toString().equals(pen.finalvalue)) {
            objCreator();
        }
    }

    @Override
    public void onClick(View arg0) {

        switch (arg0.getId()) {
        /*case R.id.doradioButton1:
            pen.timeanddistanceM();
            String Q = pen.Q;
            et.setText("Question" + Q);
            break;*/
        case R.id.doradioButton1:
            RadiobuttonAction(r1);
            break;

        case R.id.doradioButton2:
            RadiobuttonAction(r2);
            break;

        case R.id.doradioButton3:
            RadiobuttonAction(r3);
            break;

        case R.id.doradioButton4:
            RadiobuttonAction(r4);
            break;
        }
    }
}
4

5 に答える 5

0

コンテンツ ビューを次のように設定すると、ビューが見つかるはずですActivity

public RadioButton r1;
public RadioButton r2;
public RadioButton r3;
public RadioButton r4;
public EditText et;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_sums);
        //here you should retreive your views by its ids
        r1 = (RadioButton) findViewById(R.id.doradioButton1);
        r2 = (RadioButton) findViewById(R.id.doradioButton2);
        r3 = (RadioButton) findViewById(R.id.doradioButton3);
        r4 = (RadioButton) findViewById(R.id.doradioButton4);
        et = (EditText) findViewById(R.id.doeditText1);
于 2013-07-25T12:16:03.187 に答える
0

allfindViewByIdは の後に呼び出す必要がありますsetContentView。アクティビティのビュー階層が存在しない前に呼び出した場合。動く

public RadioButton r1 = (RadioButton) findViewById(R.id.doradioButton1);
public RadioButton r2 = (RadioButton) findViewById(R.id.doradioButton2);
public RadioButton r3 = (RadioButton) findViewById(R.id.doradioButton3);
public RadioButton r4 = (RadioButton) findViewById(R.id.doradioButton4);

public EditText et = (EditText) findViewById(R.id.doeditText1);


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_do_sums);
    r1 = (RadioButton) findViewById(R.id.doradioButton1);
    // etc
 }
于 2013-07-25T12:16:09.890 に答える
0

EditText以下のようにandRadioButtonsを初期化onCreate()し、マニフェストにアクティビティを追加してください

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_sums);
        r1 = (RadioButton) findViewById(R.id.doradioButton1);
        r2 = (RadioButton) findViewById(R.id.doradioButton2);
        r3 = (RadioButton) findViewById(R.id.doradioButton3);
        r4 = (RadioButton) findViewById(R.id.doradioButton4);
        et = (EditText) findViewById(R.id.doeditText1); 
    }
于 2013-07-25T12:16:30.263 に答える
0

レイアウトが作成されていないため、findViewById以前は使用できません。setContentView

于 2013-07-25T12:16:57.000 に答える
0

わかりました oncreate メソッドでこれを行う前にこれを行うことができないため、機能していません。

public EditText et = (EditText) findViewById(R.id.doeditText1);
//Button btnNextScreen = (Button) findViewById(R.id.button1);

timeanddistanceM pen = new timeanddistanceM();

それはする必要があります

public EditText et;
timeanddistanceM pen;

そしてあなたのoncreateメソッドで

et = (EditText) findViewById(R.id.doeditText1);
pen = new timeanddistanceM();

ラジオボタンでも同じことをします

于 2013-07-25T12:24:17.850 に答える