• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Studio - App closes when you press the main button even thought the code is working fine !

Gleis

Lurker
Tried many methods by changing the code, but it closes every time I press the "Try" button. You can find the Design, XML & Java code below. Also Android Studio is up to date.

The "//" is the old code I tried.
Java:
public class MainActivity extends AppCompatActivity {

    //int randomNumber;
    public void higher_lower (View view){
    EditText var1= (EditText) findViewById(R.id.TextField);
    int  var = Integer.parseInt(var1.getText().toString());

    int randomNumber;
    Random var2 = new Random();
   randomNumber =var2.nextInt(0-20)+20;

   String message;

        if(var>randomNumber){
            message ="The number is larger";
            //Toast.makeText(this, "The hidden number is larger", Toast.LENGTH_SHORT).show();
        }else if (var<randomNumber){
            message="The number is smaller";
            //Toast.makeText(this, "The hidden number is smaller", Toast.LENGTH_SHORT).show();
        }else{

            message="BINGO!";
        //Toast.makeText(this, "BINGO!", Toast.LENGTH_SHORT).show();
        }
    //Log.i("Info" , var3);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // Random var2 = new Random();
     //randomNumber = var2.nextInt(0-20)+20;

}
 

Attachments

  • Screenshot_3.png
    Screenshot_3.png
    109.3 KB · Views: 298
Hi,
A method that was executed when the button was clicked is missing in the code.

Java:
public void higher_lower(View view)
{

          //code executed on cliks
}


The name of metod you set in android : onClick in layout xml.
 
Back
Top Bottom