我的LogCat報出異常:
java.lang.RunTimeException: Unable to instantiate activity ComponentInfo{com.erikbalen.idealgaslawcalculator/com.erikbalen.idealgaslawcalculator.NumberActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity{ActivityThread.java:2121}
java文件如下:
public class NumberActivity extends Activity {
EditText pressure = (EditText) findViewById(R.id.editTextPressure);
EditText temperature = (EditText) findViewById(R.id.editTextTemperature);
EditText volume = (EditText) findViewById(R.id.editTextVolume);
TextView answer = (TextView) findViewById(R.id.textViewSolve);
Button solve = (Button) findViewById(R.id.buttonSolve);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number);
solve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double pressureValue = Double.parseDouble(pressure.getText().toString());
double temperatureValue = Double.parseDouble(temperature.getText().toString());
double volumeValue = Double.parseDouble(volume.getText().toString());
double r = 0.0821;
double answerValue = (pressureValue*volumeValue)/(r*temperatureValue);
answer.setText(Double.toString(answerValue));
}
});
}
}
請高手幫忙,謝謝。
findViewById(R.id.editTextPressure)賦值操作放在 setContentView(R.layout.activity_number);後面