-6
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   /* runOnUiThread th=new runOnUiThread(new TextChange());*/
    final TextView tv=(TextView)findViewById(R.id.tv);


            runOnUiThread(new Runnable() {
        @Override
        public void run() {

            while(true)
            {
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Random rand = new Random();
                int y=rand.nextInt(100);
                tv.setText(Integer.toString(y));

            }
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

if its a infinite loop then nothing shows up ever (it should have shown a new random number each second) and if I initiate a finite loop only the last number show when the loop gets finished. How to show a new random number on screen every second?

2 Answers2

1

I want to print a random number every second . Why does the program crash?

Because rand object of Random is null. Initialize it before calling nextInt method:

rand = new Random();
int y=rand.nextInt(100);

EDIT:

Because accessing TextView from run method of Thread which will cause Only the original thread that created a view hierarchy... so wrap :

tv.setText(Integer.toString(y));

in runOnUiThread method or use Handler.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • thanks, now at least it show 1st number , but then again it crashed @ρяσѕρєя K – kishlay raj Sep 28 '15 at 05:14
  • says Only the original thread that created a view hierarchy can touch its views and problem is in – kishlay raj Sep 28 '15 at 05:16
  • tv.setText(Integer.toString(y)); – kishlay raj Sep 28 '15 at 05:16
  • @ ρяσѕρєя K can elaborate a bit more on runOnUiThread(), bcz i m not able to do it runOnUiThread(new Runnable() { public void run() { while(true) { try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Random rand = new Random(); int y=rand.nextInt(100); tv.setText(Integer.toString(y)); } } }); – kishlay raj Sep 28 '15 at 09:26
  • Didn't understand why anybody creates a thread just to make it sleep and dispatch all task on ui-thread. Use of handler does that without creating new thread. – subhash Sep 28 '15 at 09:40
0

try:

Random rand = new Random();
int y=rand.nextInt(100);

Better way:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView tv=(TextView)findViewById(R.id.tv);

    final Handler handler = new Handler();
    final Random random = new Random();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int y = random.nextInt(100);
            tv.setText(Integer.toString(y));
            handler.postDelayed(this, 1000);
        }
    };
    handler.post(runnable);
}
subhash
  • 2,689
  • 18
  • 13