Q:

Random number generator android

ActivityMain.java
package com.abhinav.random;

import android.app. Activity;
import android.app. AlertDialog;
import android.content. DialogInterface;
import android.os. Bundle;
import android.view. View;
import android.widget. EditText;
import android.widget. Toast;

public class MainActivity extends Activity {

public int count=0;
public int rand_num;
public int n;
EditText edt_rand;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rand_num=(int)(Math.random()*100);
    edt_rand=(EditText)findViewById(R.id.edt_rand);
    n = Integer.parseInt(edt_rand.toString());
}

public void onclick(View v){
        if(count!=5){
            if(n>rand_num){
                Toast toast=Toast.makeText(this,"Your number is greater", Toast. LENGTH_SHORT);
                toast.show();
            }
            if(n<rand_num){
                Toast toast=Toast.makeText(this,"Your number is greater", Toast. LENGTH_SHORT);
                toast.show();
            }
            else{
                new AlertDialog. Builder(this)
                .setTitle("Task complete")
                .setMessage("You Win")
                .setNeutralButton("Close", new                              DialogInterface. OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
                .show();
            }
        }
        else{
            new AlertDialog. Builder(this)
            .setTitle("No Chances Left")
            .setMessage("You Lose")
            .setNeutralButton("Close", new DialogInterface. OnClickListener()       {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            })
            .show();
        }

}

}

main_activity.xml
enter code here
 <EditText 
    android:id="@+id/edt_rand"
    android:hint="@string/number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    ></EditText>

<Button
    android:id="@+id/btn_rand"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/edt_rand"
    android:layout_below="@+id/edt_rand"
    android:layout_marginTop="23dp"
    android:text="@string/click" 
    android:onClick="onclick"/>

Whats wrong with the above code? 

A:

the problem is below line in onCreate() method;
n = Integer.parseInt(edt_rand.toString());

You are actually initializing an edittext and directly fetching a value from it.  In such the case the value of n will be null or ""
modify your code like below,
 public void onclick(View v){
         n = Integer.parseInt(edt_rand.getText().toString());  // write this line here
 ...
 }

