-3

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

rcode
  • 49
  • 4

4 Answers4

2

First, i doesn't increment obviously and you have infinite loop because of that. Correct it.

Second, don't call findViewById() again and again. It is quite time taking. Call it once and initialize the TextView once in onCreate() instead.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(this);

    // Like this
    TextView textView = (TextView) findViewById(R.id.textView);

    handler=new Handler();
}

Third, do any long running operation like this in Background, like using an Async task. Ex:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

So, in your case, update the TextView with the incremented value of i in onPostExecute()(if you want to increment all at once and update once) or onProgressUpdate()(if you want to increment somewhat and post progress in between)

Recommended read: The application may be doing too much work on its main thread

Community
  • 1
  • 1
Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
1

You don't increase i variable in your code. So, while(i < 10){} is a infinity loop.

Luc Le
  • 196
  • 9
0

You haven't incremented the value of i in your code. You can do it like this:

while (i < 10) {
    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("" + i);
    i++;
    handler.postDelayed(this,1000);

}
Pradip Khomane
  • 106
  • 2
  • 7
-1

You should create a separate thread for long running actions in the context of GUI applications. Please take a look Android-API

public void onClick(View v) {
new Thread(new Runnable() {
    public void run() {
        // a potentially  time consuming task
        final Bitmap bitmap =
                processBitMap("image.png");
        mImageView.post(new Runnable() {
            public void run() {
                mImageView.setImageBitmap(bitmap);
            }
        });
    }
}).start();

}

The important thing is this:

  mImageView.post(new Runnable() {
        public void run() {
            mImageView.setImageBitmap(bitmap);
        }
    });

There is a new thread created that keeps your UI responsive and the task done at the same time.

Stimpson Cat
  • 1,444
  • 19
  • 44