I got an application who has count up to ten and show each number in a textView. Its only a sample for another Code. But the problem: when I start the app there comes nothing for a view times, and then comes a message "app isn't responding ". The log says "the application may be doing to much work on its main thread". When I searched I see there are many questions and answers to the theme "update TextView by new thread/handler..." and I've tried many examples-but nothing works. I'm new in programming and hope someone can tell me what's wrong. Maybe its easy but I don't know how to get it. Thanks for any help! Here is the code of the example:
package de.androidnewcomer.animation;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static de.androidnewcomer.animation.R.id.textView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,Runnable {
private TextView textView;
private int i;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
handler=new Handler();
}
@Override
public void run() {
count();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
count();
break;
}
}
public void count() {
while (i < 10) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("" + i);
handler.postDelayed(this,1000);
}
}
}
Edit: I update the count()- method and increased int I but now the output is only "10"... Thanks for the fast answers! here is the method now:
public void count() {
i=0;
while (i < 10) {
i++;
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("" + i);
handler.postDelayed(this,1000);
}
}
The app has to Show the numbers from one up to ten every second in the TextView. But when I start the app it shows only ten. Thanks for all help