当我尝试启动此活动时,它不会运行。
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;
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:0)
必须在findViewById
之后调用所有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
}
答案 2 :(得分:0)
在EditText
中初始化您的RadioButtons
和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);
}
答案 3 :(得分:0)
由于未创建布局,因此无法在findViewById
之前使用setContentView
。
答案 4 :(得分: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();
对您的单选按钮执行相同的操作