为什么我在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;
}
}
逻辑猫显示
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());
您可能在后台线程中调用了AsyncTask.执行()。
只能在UI线程中执行AsyncTask。
asynctask是一个在后台运行的线程,当它完成时最后只更新一次用户界面线程,但可运行线程不像asynctask,可运行线程循环调用处理程序更新用户界面线程,所以你不能把处理程序放在asynctask中。
如果您想在线程中已经运行的正在进行的任务中显示进度对话框,请尝试这种方式。它可以帮助您::
Handler mHandler=new Handler();
mHandler.post(new Runnable()
{
public void run()
{
//SHOW PROGRESS DIALOG HERE
}
});