Kiodok
Kiodok

Reputation: 19

AsyncTask with progressbar

The following code is a part of my app and it isn't working and I don't know why.

I don't know at which place I can put the "new MormaKaugummi().execute();". At any place the App crashed and gave me the error "Unfortunately, Criminal Life has stopped."

The "counter textview" is in an another activity in the same app.

(The name of the App is Criminal Life).

I need a unique solution because I'm new in Android programming.

public class Morma extends AppCompatActivity {

class MormaKaugummi extends AsyncTask<String, String, String> {

    private int count = 0;
    private void count() {
        count++;
    }

    private void update() {
        TextView counter = (TextView) findViewById(R.id.counter);
        String Kcounter = Integer.toString(count);
        counter.setText(Kcounter);
    }

    @Override
    protected String doInBackground(String... params) {
        count();
        update();
        return null;
    }
}

private ProgressBar progressBarKaugummi;
private int progressStatusKaugummi = 0;
private Handler handlerKaugummi = new Handler();

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_morma);
    Button buttonMormaKaugummiKlauen = (Button) findViewById(R.id.buttonMormaKaugummiKlauen);

    buttonMormaKaugummiKlauen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBarKaugummi = (ProgressBar) findViewById(R.id.progressBarMormaKaugummi);
            new Thread(new Runnable() {
                public void run() {
                    while (progressStatusKaugummi < 100) {
                        progressStatusKaugummi += 1;
                        handlerKaugummi.post(new Runnable() {
                            public void run() {
                                progressBarKaugummi.setProgress(progressStatusKaugummi);
                            }
                        });
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            if (progressStatusKaugummi == 100) {
                progressBarKaugummi.setProgress(0);
                recreate();
                new MormaKaugummi().execute();
            }
        }
    });

That's the logcat

11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:  Caused by: java.lang.NullPointerException
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.update(Morma.java:26)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.doInBackground(Morma.java:32)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.doInBackground(Morma.java:15)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856) 
11-07 13:48:35.514 622-622/peppermine_studios.criminallife W/EGL_emulation: eglSurfaceAttrib not implemented
11-07 13:48:35.674 622-622/peppermine_studios.criminallife W/EGL_emulation: eglSurfaceAttrib not implemented
11-07 13:48:37.364 622-1376/? I/Process: Sending signal. PID: 622 SIG: 9

Upvotes: 0

Views: 60

Answers (1)

Bhargav
Bhargav

Reputation: 8277

You are trying to do findviewbyId and update the text view in your doInBackground method via the update function, which you cannot do.

As for the crash its happening because of a null pointer exception as you can read for yourself in the logcat's caused by clause it tells you the exact line number where the null pointer exception is thrown

Caused by: java.lang.NullPointerException
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.update(Morma.java:26)

as you can see here, its caused by null pointer at line 26 in Morma.java and I am guessing that your TextView counter is null since you are trying to initialize a variable to reference a UI element from a non-UI thread, the counter variable will be initialized as null obviously.

Now for the solution:

What you need to do is put the update function in the onPostExecute method for the async task class. Reason: onPostExceute runs on whatever thread the asyntcTask.execute method was called from so you are supposed to do your UI update in the onPostExecute method

@Override
protected String doInBackground(String... params) {
    count();
    return null;
}

@Override
protect void onPostExecute(String result) {
    update();
}

Also since you are returning null from doInBackground anyway you should Extend AsyncTask<String,String,Void> ideally.

EDIT 1:

Added code

public class Morma extends AppCompatActivity {

// make the text view class variable if you want to update it outside of onCreate.
private TextView mCounterTextView;

class MormaKaugummi extends AsyncTask<Void, Void, Void> {

    private int count = 0;
    private void count() {
        count++;
    }

    private void update() {
        String Kcounter = Integer.toString(count);
        mCounterTextView.setText(Kcounter);
    }

    @Override
    protected void doInBackground(Void... aVoid) {
        count();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        update();
    }
}

private ProgressBar progressBarKaugummi;
private int progressStatusKaugummi = 0;
private Handler handlerKaugummi = new Handler();

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morma);
Button buttonMormaKaugummiKlauen = (Button) findViewById(R.id.buttonMormaKaugummiKlauen);

// initiate all your view items in onCreate.
mCounterTextView = (TextView) findViewById(R.id.counter);

buttonMormaKaugummiKlauen.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progressBarKaugummi = (ProgressBar) findViewById(R.id.progressBarMormaKaugummi);
        new Thread(new Runnable() {
            public void run() {
                while (progressStatusKaugummi < 100) {
                    progressStatusKaugummi += 1;
                    handlerKaugummi.post(new Runnable() {
                        public void run() {
                            progressBarKaugummi.setProgress(progressStatusKaugummi);
                        }
                    });
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        if (progressStatusKaugummi == 100) {
            progressBarKaugummi.setProgress(0);
            recreate();
            new MormaKaugummi().execute();
        }
    }
});

You need to initialize all your view elements like the textview in onCreate itself and keep the reference as a class variable if you are going to use that view outside of onCreate.

Upvotes: 1

Related Questions