1

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();
    }
mvd3
  • 99
  • 1
  • 5
  • 1
    `progBar` is probably null. Also you are updating ui from a thread which is wrong. Update ui on th ui thread. When orientation changes activity is destroyed and re-created – Raghunandan Mar 23 '14 at 10:39

2 Answers2

0

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

Mislav Javor
  • 1,429
  • 1
  • 15
  • 23
0

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.

Community
  • 1
  • 1
tpbapp
  • 2,506
  • 2
  • 19
  • 21
  • It works only when the app is started, it show me the exact progress bar status(1/3 in this case) but when I rotate, It shows only a small gap on the progress bar. – mvd3 Mar 23 '14 at 12:01
  • So the problem is solved, but now the activity state is not restored on orientation change? See: http://developer.android.com/guide/topics/resources/runtime-changes.html – tpbapp Mar 23 '14 at 12:40