2

I'm trying to understand the AsyncTask class, I wrote a simple code which computes if a number is Prime in a not optimized way. I don't want to compute the prime problem in the main thread, so I implemented the following class:

private class PrimeTask extends AsyncTask<Integer, Void, String>{
    public String isPrime(Integer n){
        for(int i=2; i<n-1; i++){
            if(n%i==0){
                return "Not Prime";
            }
        }
        return "Is Prime";
    }
    @Override
    protected String doInBackground(Integer ... num){
        return isPrime(num[0]);
    }
    @Override
    protected void onPostExecute(String result){
        output.setText(result);
    }
}

I compute this task every time the user presses the button in the MainActivity.

        output = findViewById(R.id.Ouput);

    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText input = findViewById(R.id.Input);
            //output.setText(isPrime(Integer.parseInt(input.getText().toString())));
            PrimeTask task = new PrimeTask();
            task.execute(Integer.parseInt(input.getText().toString()));
        }
    });

but when I run the application I still get the following message!

I/Choreographer: Skipped 57 frames!  The application may be doing too much work on its main thread

Why do I still have this message? Am I doing anything wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hamall
  • 283
  • 3
  • 13
  • 1
    Possible duplicate of [The application may be doing too much work on its main thread](https://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread) – AskNilesh Jan 03 '18 at 13:32
  • You are using the async task in a wrong way, it is meant to be for long running tasks that require disk I/0, network operations etc. Also you don't have to findViewById(R.id.Input); every time user clicks the button, initialize it in onCreate(). – Muhammad Babar Jan 03 '18 at 13:32
  • @MuhammadBabar no, he can use it for this task as well, that is not the problem. concerning calling findViewById in onClick you are right! – Willi Mentzel Jan 03 '18 at 13:41
  • 1
    @Hamall: This might help https://stackoverflow.com/a/32295725/1788806 The way you use AsyncTask is correct – Willi Mentzel Jan 03 '18 at 13:43
  • @Hamali Are you sure this is the piece of code which is causing this ? – gvmani Jan 03 '18 at 13:43
  • @gvmani there is not other code, only the onCreate() method in MainActivity. – Hamall Jan 03 '18 at 13:54
  • @MuhammadBabar I put the "findViewById(R.id.Input)" in onCreate, now there are two messages saying: Main Thread has too much work! – Hamall Jan 03 '18 at 14:02
  • @MuhammadBabar, the code inside onCreate can also cause, if you are doing something there. You might have to strip your code and see if it goes away. Else try this [https://stackoverflow.com/a/32295725/1825844] – gvmani Jan 03 '18 at 14:08

0 Answers0