How to show toast according to user input?

2k Views Asked by At

I'm new to Android Development. I'm making an app in which if the user enters a positive value less than 10, a toast should appear Red in color. If he has entered a positive value greater than 10, a toast should appear green in color. I've written the following code with no compilation errors but my app crashes at run time. Please help me out with this. Thank u :)

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)
1

There are 1 best solutions below

1
On BEST ANSWER
  1. Your app crashes on the line final int Me= Integer.parseInt(a);, since your EditText will be empty in the OnCreatemethod and an empty string cannot be parsed to an integer. You should catch the NumberFormatException
  2. You should get the text in the OnClickListener, because only then you have given the user the opportunity to enter a value in the EditText.

So your code will be:

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();
                    //}

                }
            }
        });