Question

Whenever I change the orientation, there is an error with the thread and my application closes unexpectedly.

Here is the error code

03-23 11:25:40.021: W/dalvikvm(27571): threadid=14: thread exiting with uncaught exception (group=0x40cecae0)
03-23 11:25:40.021: E/AndroidRuntime(27571): FATAL EXCEPTION: Thread-11869
03-23 11:25:40.021: E/AndroidRuntime(27571): java.lang.NullPointerException
03-23 11:25:40.021: E/AndroidRuntime(27571): at my.app.Methods$1.run(Methods.java:34) 

Here is the code for the thread :

SettingsPreferences mSettingsPreferences = new SettingsPreferences(mContext);
public void loadStatistic (final ProgressBar progBar, final SettingsPreferences settPref, final String max, final String progress, final int defaultValue) {
        Thread t = new Thread () {
            public void run() {
                try {
                    sleep(100);
                    progBar.setMax(settPref.getInt(max, defaultValue));
                    progBar.setProgress(settPref.getInt(progress, defaultValue));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }
Was it helpful?

Solution

This is because you are trying to acccess the UI on a seperate thread. You will need to use an AsyncTask thread which will enable you to periodically access the UI thread.

For example:

private class ExampleThread extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {}

    @Override
    protected void doInBackground(Void... params) {
         while(!isCancelled()) { // Keep going until cancelled
             try {
                 Thread.sleep(100); // Delay 100 milliseconds
             } catch (InterruptedException e) {
                 Thread.interrupted();
             }
             publishProgress(); // Run onProgressUpdate() method
             if(isCancelled()) break; // Escape early if cancel() is called
         }

    }

    @Override
    protected void onPostExecute(Void... params) {}

    @Override
    protected void onProgressUpdate(Void... params) {
        // Here you can access the UI thread
        progBar.setMax(settPref.getInt(max, defaultValue));
        progBar.setProgress(settPref.getInt(progress, defaultValue));
    }
}

To start the thread:

ExampleThread thread = new ExampleThread();
thread.execute();

To stop the thread:

thread.cancel();

More information and examples with AsyncTask can be found on this question.

OTHER TIPS

You must update all of your UI elements on the main or UI thread. I would recommend creating an AsyncTask subclass and then implementing onPreExecute and onPostExecute to start and stop the progress bar or to measure times. Then do all of your UI things on runOnUiThread . runOnUiThread takes a Runnable as an argument so you can do everything your heart desires

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top