-1

Im generating 2 random values and in EditText you need to enter a result of adding this 2 values and when im tring to parse that what i get from edittext it crash my application

public class Main2Activity extends AppCompatActivity {

public int wynik;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setContentView(R.layout.activity_main2);
    TextView text = (TextView) findViewById(R.id.txtv);

    Random r = new Random();
    int i1 = (r.nextInt(100 - 1) + 1);
    int i2 = (r.nextInt(100 - 1) + 1);
    text.setText(i1 +" + " + i2 + " = ");
    wynik =i1+i2;
};
    public void backtomain(View view)
    {
        EditText editText = (EditText) findViewById(R.id.edittext);
        String etstring =editText.getText().toString();
        Integer etint = Integer.parseInt(etstring); <---------------- CRASH CUZ OF THIS LINE

        //if(etint==wynik)
        //{
            Intent intent = new Intent(this, AalarmActivity.class);
            intent.putExtra("toggleBtn", true);
            startActivity(intent);
            finish();
        //}
        //else
        //{
        //    Toast.makeText(this, "Error ",
        //            Toast.LENGTH_LONG).show();
        //}
    }
}

Commented lines are not finished first need to repair that parsing part.

1 Answers1

3

You probably is receiving a input that is not a number.

You can force to use the input type as mumber, modify your EditText in XML and include this property:

android:inputType="number" 

This will force to user input only numbers, and your Integer.parseInt(etstring); should parse the number without format exceptions.

Deividi Cavarzan
  • 10,034
  • 13
  • 66
  • 80