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

发布于 2024-11-29 17:14:10 字数 3691 浏览 1 评论 0原文

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

我有一个

  1. MainActivity 扩展了 Second Activity
  2. 中的内部类
  3. AsyncTask 作为 MainActivity Second Activity

Main Activity 和 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.....}

这有什么错误代码,请帮我解决这个问题
我遇到以下错误

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-12-06 17:14:10

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文