线程内不能创建一个没有调用Looper

线程内不能创建一个没有调用Looper

本文介绍了线程内不能创建一个没有调用Looper.prepare()的AsyncTask对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中有一个切换按钮。我希望每次更改切换按钮以取消选中时,出现一个对话框,当您按下是的时候,Facebook的墙上发布了一条消息。

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上没有Facebook的对话框

I took code for publishing on facebook without facebook's dialog here https://stackoverflow.com/a/4376415/1403979

这是我的代码:

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

}

编辑: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");
                                    }});

为什么要尝试运行 AsyncTask 使用 runOnUiThread()

Why are you trying to run a AsyncTask using the runOnUiThread()?

您可以简单地调用 OnClick() AsyncTask c $ c>这样的方法:

You are suppose to simply call the AsyncTask in the OnClick() method like this:

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

然后显示一个 progressdialog 在您的 AsyncTask onPreExecute()中,将在UI-Thread上显示,而 doInBackground()将发生在非UI线程的后台。

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.

这篇关于线程内不能创建一个没有调用Looper.prepare()的AsyncTask对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:32