无法在AsyncTask中未调用Looper.prepare()的线程内创建处理程序

时间:2011-08-15 05:55:48

标签: android android-asynctask fbconnect

因为我的应用很大程度上依赖于网络,所以我希望我的应用不要挂断。 所以我尝试使用Asyncask

我有一个

  1. MainActvity扩展第二个活动
  2. AsyncTask作为MainActivity中的内部类
  3. 第二项活动
  4. 主要活动和异步任务:

    public class MainActivity extends secondActivity implements OnClickListener{
    @Override
    public void onClick(View arg0) {
        new triggerAsync().execute(str);
    }
    
    private class triggerAsync extends AsyncTask<String,String,String>
    {
        @Override       
        protected String doInBackground(String... str) {
    
            try {
                 setConnection();
                 getID(txtUserName);
                 Log.d("Inside Background","string here"+str[0]);
                // postOnWall(str[0]);  
            } catch (Exception e) {
                e.printStackTrace();
            }   
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            super.onPostExecute(result);
        }}}
    

    第二项活动:

    public class SecondActivity extends Activity {
    
    
    public void setConnection() {
        //My stuffs
    }
    
    public void getID(TextView txtUserName) {
    
        // here i'm calling another function inside package
    
        if (isSession()) {
            Log.d(TAG, "sessionValid");
            mAsyncRunner.request("me", new IDRequestListener());
        } else {
            // no logged in, so relogin
            Log.d(TAG, "sessionNOTValid, relogin");
            mFacebook.authorize(this, PERMS, new LoginDialogListener());
        } 
    }
    
    /////////////// Code continues.....}
    

    此代码中的错误是什么,请帮我解决这个问题 我收到了以下错误

    08-15 11:01:07.211: WARN/System.err(462): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    08-15 11:01:07.251: WARN/System.err(462):     at android.os.Handler.<init>(Handler.java:121)
    08-15 11:01:07.251: WARN/System.err(462):     at android.app.Dialog.<init>(Dialog.java:101)
    08-15 11:01:07.271: WARN/System.err(462):     at android.app.Dialog.<init>(Dialog.java:123)
    08-15 11:01:07.271: WARN/System.err(462):     at com.facebook.android.FbDialog.<init>(FbDialog.java:63)
    08-15 11:01:07.281: WARN/System.err(462):     at com.facebook.android.Facebook.dialog(Facebook.java:622)
    08-15 11:01:07.291: WARN/System.err(462):     at com.facebook.android.Facebook.startDialogAuth(Facebook.java:297)
    08-15 11:01:07.301: WARN/System.err(462):     at com.facebook.android.Facebook.authorize(Facebook.java:195)
    08-15 11:01:07.311: WARN/System.err(462):     at com.facebook.android.Facebook.authorize(Facebook.java:114)
    08-15 11:01:07.321: WARN/System.err(462):     at com.singleupdater.FacebookActivity.getID(FacebookActivity.java:61)
    08-15 11:01:07.331: WARN/System.err(462):     at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:149)
    08-15 11:01:07.341: WARN/System.err(462):     at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:1)
    08-15 11:01:07.351: WARN/System.err(462):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    08-15 11:01:07.361: WARN/System.err(462):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    08-15 11:01:07.371: WARN/System.err(462):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    08-15 11:01:07.371: WARN/System.err(462):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    08-15 11:01:07.391: WARN/System.err(462):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    08-15 11:01:07.391: WARN/System.err(462):     at java.lang.Thread.run(Thread.java:1096)
    

1 个答案:

答案 0 :(得分:1)

似乎您的某些方法 - setConnection()getID(txtUserName)尝试从后台线程访问UI并失败。将UI逻辑移动到onPostExecute以在UI线程上处理。