为什么我在使用此代码时遇到此错误,asynctask
即使我正在使用 runOnUiThread()
这是我正在使用的代码(我正在使用片段)。
private class GmailXMPP_AfterLogin extends AsyncTask<AfterLogging, Void, Void> {
private ProgressDialog _dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
_dialog = new ProgressDialog(getActivity());
_dialog.setCancelable(false);
_dialog.setTitle("colors");
//((ProgressDialog)dialog).setIcon(R.drawable.icon);
_dialog.setMessage("Your content is loading.. \nPlease wait..");
_dialog.setIndeterminate(false);
_dialog.show();
//ProgressBar_show();
Log.i(TAG, " GmailXMPP_AfterLogin onPreExecute" );
}
@Override
protected void onPostExecute(Void feed) {
//ProgressBar_hide();
_dialog.dismiss();
Log.i(TAG, " GmailXMPP_AfterLogin onPostExecute" );
}
@Override
protected Void doInBackground(AfterLogging... arg0) {
Log.i(TAG, " GmailXMPP_AfterLogin doInBackground" );
AfterLogging after = arg0[0];
final XMPPConnection _connection = after.getConn();
final String Uname = after.getAcc_Name();
//synchronized (contactJids) {
Roster roster = Gtalk_xmpp_client.getRoster();
String file_name;
for (RosterEntry entry : roster.getEntries()) {
if (entry.getType() == ItemType.to || entry.getType() == ItemType.both) {
boolean yes = Contact_data_source.checkUsername(entry.getUser());
Log.i(TAG, "Con=" + yes);
if (!yes) {
Bitmap buddy_img = buddyImage(entry, _connection);
if (buddy_img != null)
file_name = Store(buddy_img);
else
file_name = "";
if (entry.getName() == null)
Contact_data_source.createContact( entry.getUser(), entry.getUser(), Uname, file_name, UsedStrings.GoogleAccount );
else
Contact_data_source.createContact( entry.getName(), entry.getUser(), Uname, file_name, UsedStrings.GoogleAccount );
} else {
Log.i(TAG, "Con=exist");
}
}
}
return null;
}
}
logcat 显示
12-01 11:59:21.549: E/AndroidRuntime(16595): FATAL EXCEPTION: AsyncTask #1
12-01 11:59:21.549: E/AndroidRuntime(16595): java.lang.RuntimeException: An error occured while executing doInBackground()
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.lang.Thread.run(Thread.java:1019)
12-01 11:59:21.549: E/AndroidRuntime(16595): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.os.Handler.<init>(Handler.java:121)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.app.Dialog.<init>(Dialog.java:101)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.app.AlertDialog.<init>(AlertDialog.java:63)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
12-01 11:59:21.549: E/AndroidRuntime(16595): at com.pipemedia.fragments.ChatAccountsFragment$GmailXMPP_AfterLogin.onPreExecute(ChatAccountsFragment.java:372)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.os.AsyncTask.execute(AsyncTask.java:391)
12-01 11:59:21.549: E/AndroidRuntime(16595): at com.pipemedia.fragments.ChatAccountsFragment.Gtalk_logInComplete(ChatAccountsFragment.java:435)
12-01 11:59:21.549: E/AndroidRuntime(16595): at com.pipemedia.xmpp.GmailXmppClient.Login(GmailXmppClient.java:60)
12-01 11:59:21.549: E/AndroidRuntime(16595): at com.pipemedia.fragments.ChatAccountsFragment$GmailXMPP.doInBackground(ChatAccountsFragment.java:337)
12-01 11:59:21.549: E/AndroidRuntime(16595): at com.pipemedia.fragments.ChatAccountsFragment$GmailXMPP.doInBackground(ChatAccountsFragment.java:1)
12-01 11:59:21.549: E/AndroidRuntime(16595): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-01 11:59:21.549: E/AndroidRuntime(16595): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-01 11:59:21.549: E/AndroidRuntime(16595): ... 4 more
这是创建错误的行
line 372 : progress_dialog = new ProgressDialog(getActivity());