Johan
Johan

Reputation: 51

Android: Dialog crashes in Thread?

I have a class with a thread and a progress dialog. When the thread stops, the dialog must dismiss. But if the thread stops, the app crashes :S Has anyone an idea whats wrong?


public class Main extends Activity {

public static ProgressDialog LoadingDialog = null;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LoadingDialog.show(AndroidRSSReader.this, "Laden...", "Even geduld aub...", true); setContentView(R.layout.main); startUp();

new Thread(new Runnable(){ public void run() {

         try {
 Thread.sleep(1000);
} catch (InterruptedException e) {

 e.printStackTrace();
}
         LoadingDialog.dismiss();
        }
    }).start();

}

Upvotes: 0

Views: 1542

Answers (2)

100rabh
100rabh

Reputation: 6186

Seems you have problems in dismissing a dialog try using a Handler to perform an action on UI thread :

private Handler handler = new Handler() {
    @Override

    public void handleMessage(Message msg) {
    // perform logic
    if(LoadingDialog!=null)//first check if dialog is not null.This might be a reason for crashing
     LoadingDialog.dismiss();
    LoadingDialog=null
    }

    };

& then call it in your activity by simply calling handler.sendEmptyMessage(0); &you are done.

Additional advice :also have a look at AsyncTask to perform async operation.

Upvotes: 1

Josh Clemm
Josh Clemm

Reputation: 2966

LoadingDialog is still null when you call dismiss. You need to make sure and assign it to something (like your progress bar).

LoadingDialog = ProgressDialog.show(AndroidRSSReader.this, "Laden...", "Even geduld aub...", true);

Upvotes: 3

Related Questions