简体   繁体   English

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

[英]Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog

In my activity there is a toggle button. 在我的活动中有一个切换按钮。 I want every time you change the toggle button to unchecked a dialog box appear and when you press on Yes a message is published on the facebook's wall. 我希望每次更改切换按钮以取消选中时都会出现一个对话框,当您按下时,会在Facebook的墙上发布一条消息。

I took code for publishing on facebook without facebook's dialog here https://stackoverflow.com/a/4376415/1403979 我在Facebook上发布代码用于发布没有facebook的对话框https://stackoverflow.com/a/4376415/1403979

Here is my code: 这是我的代码:

OnCheckedChangeListener toggleButtonListener = new OnCheckedChangeListener() {
    // called when user toggles session state
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
            if (!isChecked) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    SocialFootActivity.this);
            builder.setMessage(
                    "Do you want to share on facebook")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {           
                                    SocialFootActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                    new PostMsg().execute("Hello World");
                                        }});
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();

        } else {
            //do somethings
        }
    }
};

AsyncTask 的AsyncTask

public class PostMsg extends AsyncTask<String,Void,Void>{

@Override
protected Void doInBackground(String... msg) {
    // TODO Auto-generated method stub
    try {
        SocialFootActivity sf = new SocialFootActivity();
        String response = sf.facebook.request("me");
        Bundle parameters = new Bundle();
        parameters.putString("message", msg[0]);
        parameters.putString("description", "test test test");
        response = sf.facebook.request("me/feed", parameters, "POST");
        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("")
                || response.equals("false")) {
            Log.v("Error", "Blank response");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

EDIT:Logcat 编辑:logcat的

06-29 17:37:19.055: W/System.err(24427): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-29 17:37:19.155: D/dalvikvm(24427): GC_CONCURRENT freed 1080K, 25% free 6038K/8003K, paused 2ms+5ms
06-29 17:37:19.175: W/CursorWrapperInner(24427): Cursor finalized without prior close()
06-29 17:37:19.175: W/System.err(24427):    at android.os.Handler.<init>(Handler.java:121)
06-29 17:37:19.175: W/System.err(24427):    at android.app.Activity.<init>(Activity.java:735)
06-29 17:37:19.175: W/System.err(24427):    at com.google.android.maps.MapActivity.<init>(MapActivity.java:272)
06-29 17:37:19.185: W/System.err(24427):    at it.univpm.dii.socialfoot.SocialFootActivity.<init>(SocialFootActivity.java:56)
06-29 17:37:19.185: W/System.err(24427):    at it.univpm.dii.socialfoot.PostMsg.doInBackground(PostMsg.java:13)
06-29 17:37:19.185: W/System.err(24427):    at it.univpm.dii.socialfoot.PostMsg.doInBackground(PostMsg.java:1)
06-29 17:37:19.185: W/System.err(24427):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-29 17:37:19.185: W/System.err(24427):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-29 17:37:19.185: W/System.err(24427):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-29 17:37:19.195: W/System.err(24427):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-29 17:37:19.195: W/System.err(24427):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-29 17:37:19.195: W/System.err(24427):    at java.lang.Thread.run(Thread.java:856)

There is an error in this line of your code: 这行代码中存在错误:

SocialFootActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                new PostMsg().execute("Hello World");
                                    }});

Why are you trying to run a AsyncTask using the runOnUiThread() ? 为什么要尝试使用runOnUiThread()运行AsyncTask

You are suppose to simply call the AsyncTask in the OnClick() method like this: 您可以简单地在OnClick()方法中调用AsyncTask ,如下所示:

OnCheckedChangeListener toggleButtonListener = new OnCheckedChangeListener() {
// called when user toggles session state
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
        if (!isChecked) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                SocialFootActivity.this);
        builder.setMessage(
                "Do you want to share on facebook")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                new PostMsg().execute("Hello World");
                                    });
                            }

Then show a progressdialog in the onPreExecute() of your AsyncTask which will be shown on the UI-Thread while your doInBackground() will happen in the background non-UI thread. 然后在AsyncTaskonPreExecute()中显示progressdialog ,它将显示在UI-Thread上,而doInBackground()将在后台非UI线程中显示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog 使用AsyncTask时无法在未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() when using AsyncTask asynctask中的CursorLoader,无法在未调用Looper.prepare的线程内创建处理程序 - CursorLoader in asynctask, can't create handler inside thread that has not called Looper.prepare 无法在 AsyncTask 中未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() in AsyncTask Asynctask导致异常“无法在未调用Looper.prepare()的线程内创建处理程序” - Asynctask causes exception 'Can't create handler inside thread that has not called Looper.prepare()' Android AsyncTask [无法在未调用Looper.prepare()的线程内创建处理程序 - Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()] 无法在未调用looper.prepare()asynctask的线程内创建处理程序 - Can't create handler inside thread that has not called looper.prepare() asynctask 无法在尚未使用AsyncTask调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() with AsyncTask Android AsyncTask无法在未调用Looper.prepare()的线程内创建处理程序? - Android AsyncTask Can't create handler inside thread that has not called Looper.prepare()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM