我是Android开发的新手。我正在制作一个应用程序,如果用户输入小于10的正值,则吐司应显示为红色。如果他输入的正值大于10,则吐司应显示为绿色。我编写了以下代码,没有编译错误,但我的应用程序在运行时崩溃。这个你能帮我吗。谢谢你:)
public class SecondScreen extends ActionBarActivity {
Button sub;
EditText mEdit1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
sub = (Button) findViewById(R.id.sub);
mEdit1 = (EditText) findViewById(R.id.editText1);
String a= mEdit1.getText().toString();
final int Me= Integer.parseInt(a);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//setting minimum distance to be 10cm
if (Me > 10 && Me >= 0) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Perfect!!");
// Toast configuration
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.getView().setBackgroundColor(Color.GREEN);
toast.show();
} else {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Stop!! Stop!!");
// Toast configuration
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.getView().setBackgroundColor(Color.RED);
toast.show();
//}
}
}
});
}
}
11-22 22:31:55.214 732-732/com.cpa.hooriya.cpa3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cpa.hooriya.cpa3/com.cpa.hooriya.cpa3.SecondScreen}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:359)
at java.lang.Integer.parseInt(Integer.java:332)
at com.cpa.hooriya.cpa3.SecondScreen.onCreate(SecondScreen.java:57)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
final int Me= Integer.parseInt(a);
行崩溃,因为您的EditText在OnCreate
方法中将为空,并且无法将空字符串解析为整数。你应该抓住NumberFormatException
OnClickListener
中获取文字,因为只有这样您才能让用户在EditText
中输入值。所以你的代码将是:
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEdit1 = (EditText) findViewById(R.id.editText1);
String a= mEdit1.getText().toString();
int me = 0;
try{
me= Integer.parseInt(a);
}catch(NumberFormatException e){
//me will be 0 if the user does not enter a valid number
e.printStackTrace();
}
//setting minimum distance to be 10cm
// the Me >= 0 check is not needed, since if it is larger then 10
//it is larger then 0
if (Me > 10) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Perfect!!");
// Toast configuration
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.getView().setBackgroundColor(Color.GREEN);
toast.show();
} else {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Stop!! Stop!!");
// Toast configuration
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.getView().setBackgroundColor(Color.RED);
toast.show();
//}
}
}
});