简体   繁体   中英

App crash when using ProgressDialog and Threads

It's my first experience in using ProgressDialog and Threads. When I try to use ProgressDialog app crash.

ProgressDialog and Thread code:

 pd = ProgressDialog.show(MainScreen.this, "Backup", "Please, wait...", true, true);
     final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    pd.dismiss();

            }
    };
    new Thread (new Runnable() { 
         public void run() {
             Looper.prepare();
                DB = new DataBase(MainScreen.this);
                Cursor r = DB.fetchAllNames();
                if (r.getCount() == 0){
                    File sdPath = Environment.getExternalStorageDirectory();
                    File sdPathDr = new File(sdPath.getAbsolutePath() + "/Pass_backup/" + DIR_SD);
                    if(!sdPathDr.exists()){
                    }
                    else {
                        File[] files = sdPathDr.listFiles();
                        if (files.length == 0){
                        }
                        else {
                            try {
                                restoring();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        }
                 }
             }   
             else {
                try {
                    backupAdapter();
                } catch (IOException e1) {
                    e1.printStackTrace();
                } 
                try {
                    restoring();
                } catch (IOException e) {
                    e.printStackTrace();
                    }
             }
                handler.sendEmptyMessage(0);
            }}).start(); 

The code inside thread work, but then app crash.

LogCat:

03-29 15:49:06.754: E/AndroidRuntime(12608): FATAL EXCEPTION: Thread-8781
03-29 15:49:06.754: E/AndroidRuntime(12608): Process: com.ssd.passwordbook, PID: 12608
03-29 15:49:06.754: E/AndroidRuntime(12608):android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.View.invalidate(View.java:10942)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.View.invalidate(View.java:10897)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.widget.ImageView.invalidateDrawable(ImageView.java:201)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:344)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.graphics.drawable.Drawable.setVisible(Drawable.java:575)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.widget.ImageView.onDetachedFromWindow(ImageView.java:1243)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.View.dispatchDetachedFromWindow(View.java:12627)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2585)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4027)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.widget.AbsListView.resetList(AbsListView.java:1924)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.widget.ListView.resetList(ListView.java:521)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at android.widget.ListView.setAdapter(ListView.java:462)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at com.ssd.passwordbook.MainScreen.loaderAdapter(MainScreen.java:930)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at com.ssd.passwordbook.MainScreen.restoring(MainScreen.java:1068)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at com.ssd.passwordbook.MainScreen$9.run(MainScreen.java:728)
03-29 15:49:06.754: E/AndroidRuntime(12608):    at java.lang.Thread.run(Thread.java:841)

So, how can I solve this problem?

Take a look at the AsyncTask Class. http://developer.android.com/reference/android/os/AsyncTask.html

Its made for these kind of things.

onPreExecute() runs on the UI thread, so you can touch the UI there, like showing a progressdialog.

doInBackground(Params...) runs in a background thread, so you can do your heavy lifting here.

onPostExecute(Result) runs on the UI thread, so you can show the result from the background thread here. And also. turn off the progressdialog :)

Are you doing ui work inside your thread.If it is, do UI works with using

runOnUiThread(new Runnable() {
    public void run() {
       //UI Works
    }
});

Only the original thread that created a view hierarchy can touch its views.

This error message means that you are creating the Progress Dialog on the main thread:

pd = ProgressDialog.show(MainScreen.this, "Backup", "Please, wait...", true, true);
pd.show();

But dismissing it on a different thread:

 handler.sendEmptyMessage(0);

As per the Android Framework, you ought to use the AsyncTask for such a thing. It has a onProgressUpdate() callback that you can use to update the ProgressBar. An example can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM