问题描述
- 得到了RunTimeException异常
-
我的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);后面
解决方案二:
你在androidmanifest中添加下面三个Activity:
?<activity?
android:name=".****sActivity"??
android:label="@string/app_name">
</activity>?
<activity?
android:name=".*****Activity"?
android:label="@string/app_name">
</activity>?
<activity?android:name=".*****Activity"?
?android:label="@string/app_name">
</activity>
*****是你的Activity
解决方案三:
java.lang.NullPointerException
空指针 你检查下....
解决方案四:
我也遇到了同样的报错,但是我是因为在Manifest.xml中更改了包名 ,但是在 android:name=" .MainActivity" 处没有添加包名
因为我的.java文件是在com.test.login包下的,所以应改为 android:name=" com.test.login.MainActivity" ~~~
给可能会有同样问题的童鞋们~
时间: 2024-10-27 07:41:26