-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必须调用 after 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

像下面一样初始化你的EditTextRadioButtonsonCreate()并确保你在清单中添加你的活动

    @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

由于未创建布局,因此您不能使用findViewByIdbefore 。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 回答