0

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.

I took code for publishing on facebook without facebook's dialog here 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

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

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)
Community
  • 1
  • 1
Andrea C.
  • 109
  • 4
  • 12

1 Answers1

2

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()?

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

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.

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • processdialog is necessary??? I have tried as you suggest (without process dialog in onpreexecute) and I have the same error – Andrea C. Jun 29 '12 at 15:58
  • If you don't show a progressDialog and if ever it takes more time to fetch response from the server then you may get an ANR exception. – Arun George Jun 29 '12 at 16:06