【问题标题】:How to show toast according to user input?如何根据用户输入显示吐司?
【发布时间】:2014-11-22 22:55:57
【问题描述】:

我是 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)

【问题讨论】:

  • 把你的logcat报错。
  • @MohammadRahchamani 如果我删除 if-else 条件,该应用程序可以正常工作。

标签: android class if-statement android-toast


【解决方案1】:
  1. 您的应用程序在final int Me= Integer.parseInt(a); 行崩溃,因为您的EditText 在OnCreate 方法中将为空,并且空字符串无法解析为整数。你应该赶上NumberFormatException
  2. 您应该在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();
                    //}

                }
            }
        });

【讨论】:

  • 非常感谢您。我已经研究了一个小时,但找不到解决方案。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2014-05-13
  • 2019-10-17
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
相关资源
最近更新 更多