以下行,无论放在哪里都会使我的 Android 程序崩溃。
EditText editText1;
double pro = Double.parseDouble(editText1.getText().toString());
附加代码:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btsub"
android:layout_marginTop="58dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
我究竟做错了什么?我没有调试经验。
编辑 -
下面将显示一个包含:“Something is real wrong”的吐司
try {
if(editText1 != null) {
pro = Double.parseDouble(editText1.getText().toString());
} else {
Context context = getApplicationContext();
CharSequence text = "Something real wrong";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
} catch(NumberFormatException e) {
Context context = getApplicationContext();
CharSequence text = "Empty";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}