开发者

Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask

Because my app depends much on Network, I wish my app should not hang up. So i tried to use Asyncask

I have a

  1. MainActvity extends Second Activity
  2. AsyncTask as Inner Class in MainActivity
  3. Second Activity

Main Activity and Async Task :

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);
    }}}

Second Activity :

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.....}

What is the error in this code, please help me to solve this

I'm getting below error

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)


It seems that some of your methods - setConnection() or getID(txtUserName) tries to access UI from background thread and fails. Move your UI logic to onPostExecute to be processed on UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜