问题描述
我有一个EditText,按钮和TextView的一个活动。如果我写一些数字,然后单击按钮,TextView的显示计算结果。
I have an Activity with an EditText, Button and TextView. If I write some numbers and click the button, the TextView shows the result of the calculation.
如果该EditText上是空的(即没有数字输入的),我按一下按钮我有一个对话框说法:应用程序已意外停止,请重试。
If the EditText is empty (i.e. there are no numbers entered) and I click the Button I have the a dialog saying: "The application has stopped unexpectedly. Please try again".
请帮助我。我想显示同(例如,插入数字)的一些意见的一个窗口,如果用户不写任何数字。我应该如何编程的EditText领域?
如何解决此问题?非常感谢
Please help me. I want to show a window with some comments (for example "Insert numbers"), if the user does not write any numbers. How should I program the EditText field?How to solve this? Thanks a lot
这是我的onClick方法:
This is my onClick method:
public void onClick(View v) {
EditText numA = (EditText)findViewById(R.id.editText1);
TextView wynik1 = (TextView)findViewById(R.id.wynik);
float num1 = Float.parseFloat(numA.getText().toString());
float eq1 = 0;
if(num1>0){
switch (v.getId()) {
case R.id.oblicz:
eq1 = 2*num1;
break;
}
wynik1.setText(String.format("%f", eq1));
}
else {
Intent o = new Intent(this, Obliczokno.class);
startActivity(o);
}
}
我已经改变的onClick方法:
I have changed onClick method:
public void onClick(View v) {
EditText numA = (EditText)findViewById(R.id.editText1);
float num2 = Float.parseFloat(numA.getText().toString());
float eq1 = 0;
float eq2 = 0;
try{ float num1 = Float.parseFloat(numA.getText().toString());
if(num1>0 || num2>0){
switch (v.getId()) {
case R.id.pole:
eq1 = 2*num1 ;
break;
case R.id.obwod:
eq2 = 3*num1 ;
break;
}}
else {Intent o = new Intent(this, Obliczokno.class);
startActivity(o);}
}
catch (NumberFormatException e) {
Toast.makeText(this, "Sorry you did't type anything", Toast.LENGTH_SHORT).show();
return;
}
和我有错误。我的LogCat中的错误:
and I have errors. My LogCat errors:
E/AndroidRuntime(321): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(321): java.lang.NumberFormatException:
E/AndroidRuntime(321): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:296)
E/AndroidRuntime(321): at java.lang.Float.parseFloat(Float.java:327)
E/AndroidRuntime(321): at arek.geometria.Oblicz.onClick(Oblicz.java:35)
E/AndroidRuntime(321): at android.view.View.performClick(View.java:2344)
E/AndroidRuntime(321): at android.view.View.onTouchEvent(View.java:4133)
E/AndroidRuntime(321): at android.widget.TextView.onTouchEvent(TextView.java:6510)
E/AndroidRuntime(321): at android.view.View.dispatchTouchEvent(View.java:3672)
E/AndroidRuntime(321): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(321): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(321): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(321): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
E/AndroidRuntime(321): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
E/AndroidRuntime(321): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
E/AndroidRuntime(321): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
E/AndroidRuntime(321): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
E/AndroidRuntime(321): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
E/AndroidRuntime(321): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(321): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(321): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(321): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(321): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(321): at dalvik.system.NativeStart.main(Native Method)
如果在别的...正常工作。
你有什么想法?请帮我。
The if ... else works correctly.Have you got any ideas? Please help me.
推荐答案
您只需要做哪些用户键入一个检查:
You just need to do a check on what the user has typed:
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
}
的修改的
如果你有一个浮动,你有你解析浮动之前做的检查:
If you have a float, you have to do the check before you parse the float:
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
// Toast message
return;
}
float num1 = Float.parseFloat(input);
或
try{
float num1 = Float.parseFloat(numA.getText().toString());
} catch (NumberFormatException e) {
// Toast message - you cannot get a float from that input
return;
}
这篇关于发行空的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!