在我的活动中有一个切换按钮。我希望每次更改切换按钮以取消选中时都会出现一个对话框,当您按下时,会在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
}
}
};
的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)
答案 0 :(得分: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");
});
}
然后在progressdialog
onPreExecute()
显示AsyncTask
,doInBackground()
将显示在UI-Thread上,而{{1}}将在后台非UI线程中显示