2

为什么我在使用此代码时遇到此错误,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());
4

4 回答 4

4

可能您在后台线程中调用了 AsyncTask.execute() 。

您只能在 UI 线程中执行 AsyncTask。

于 2012-12-01T06:39:09.510 回答
0

asynctask 是一个在后台运行的线程,完成后只更新一次 ui 线程,但可运行线程不像 asynctask,可运行线程调用处理程序以循环更新 ui 线程,因此您不能将处理程序放入 asynctask .

于 2012-12-01T05:43:49.877 回答
0

如果您想在已经在线程中运行的正在进行的任务中显示进度对话框,请尝试这种方式。可能对您有帮助::

Handler mHandler=new Handler();

mHandler.post(new Runnable() 
{
     public void run()
     {
          //SHOW PROGRESS DIALOG HERE                           
     }
});
于 2012-12-01T06:34:43.250 回答
0

创造

progressDialog = ProgressDialog.show(SplashScreen.this, "Please wait...", "Porcessing ...", true);

在要启动 AsyncTask 的 Activity 类中。

于 2012-12-01T06:55:55.767 回答