0

在我的活动中有一个切换按钮。我希望每次您将切换按钮更改为未选中时都会出现一个对话框,并且当您按下“是”时,会在 Facebook 的墙上发布一条消息。

我在没有 facebook 对话框的情况下在 facebook 上发布代码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
        }
    }
};

异步任务

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)
4

1 回答 1

2

这行代码中有一个错误:

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

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

您应该像这样简单地调用AsyncTask方法OnClick()

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

然后progressdialogonPreExecute()你的中显示一个,AsyncTask这将显示在 UI 线程上,而你doInBackground()将发生在后台非 UI 线程中。

于 2012-06-29T15:29:01.833 回答