这是我第一次使用ProgressDialog和Threads。当我尝试使用ProgressDialog应用程序时崩溃。
ProgressDialog和线程代码:
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();
线程内的代码正常工作,但随后应用崩溃。
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)
那么,我该如何解决这个问题呢?
最佳答案
您是否正在线程内进行ui工作?
runOnUiThread(new Runnable() {
public void run() {
//UI Works
}
});
关于java - 使用ProgressDialog和Threads时应用崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22732175/