一个对话框,里面的AsyncTask - 内螺纹无法创建处理已经不叫尺蠖prepare()。 [英] Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog

查看:257
本文介绍了一个对话框,里面的AsyncTask - 内螺纹无法创建处理已经不叫尺蠖prepare()。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动有一个切换按钮。我希望每一次更改切换按钮,不选中对话框出现,当你是preSS的消息公布在Facebook的墙壁上。

我拿了code在Facebook上发布没有Facebook的对话框这里 http://stackoverflow.com/a/4376415/1403979

下面是我的code:

  OnCheckedChangeListener toggleButtonListener =新OnCheckedChangeListener(){
    //调用时用户切换会话状态
    @覆盖
    公共无效onCheckedChanged(CompoundButton buttonView,
            布尔器isChecked){
            如果(!器isChecked){
            AlertDialog.Builder建设者=新AlertDialog.Builder(
                    SocialFootActivity.this);
            builder.setMessage(
                    你想在Facebook上共享)
                    .setCancelable(假)
                    .setPositiveButton(是,
                            新DialogInterface.OnClickListener(){
                                公共无效的onClick(DialogInterface对话框,
                                        INT ID){
                                    SocialFootActivity.this.runOnUiThread(新的Runnable(){
                                        @覆盖
                                        公共无效的run(){
                                    新PostMsg()执行(的Hello World);
                                        }});
                                }
                            })
                    .setNegativeButton(否,
                            新DialogInterface.OnClickListener(){
                                公共无效的onClick(DialogInterface对话框,
                                        INT ID){
                                    dialog.cancel();
                                }
                            });
            AlertDialog警报= builder.create();
            alert.show();        }其他{
            //做出头
        }
    }
};

的AsyncTask

 公共类PostMsg扩展的AsyncTask<弦乐,太虚,太虚> {@覆盖
保护无效doInBackground(字符串...味精){
    // TODO自动生成方法存根
    尝试{
        SocialFootActivity SF =新SocialFootActivity();
        字符串的响应= sf.facebook.request(我);
        捆绑参数=新包();
        parameters.putString(信息,味精[0]);
        parameters.putString(说明,测试测试测试);
        响应= sf.facebook.request(ME /饲料,参数,POST);
        Log.d(测试,得到了回应:+响应);
        如果(响应==空|| response.equals()
                || response.equals(假)){
            Log.v(错误,空白响应);
        }
    }赶上(例外五){
        e.printStackTrace();
    }
    返回null;
}}

编辑:logcat的

  06-29 17:37:19.055:W / System.err的(24427):了java.lang.RuntimeException:无法内螺纹已不叫尺蠖创造处理器prepare()
06-29 17:37:19.155:D / dalvikvm(24427):GC_CONCURRENT释放1080K,25%的自由6038K / 8003K,暂停2MS + 5ms的
06-29 17:37:19.175:W / CursorWrapperInner(24427):没有事先关闭游标敲定()
06-29 17:37:19.175:W / System.err的(24427):在android.os.Handler<&初始化GT;(Handler.java:121)
06-29 17:37:19.175:W / System.err的(24427):在android.app.Activity<&初始化GT;(Activity.java:735)
06-29 17:37:19.175:W / System.err的(24427):在com.google.android.maps.MapActivity<&初始化GT;(MapActivity.java:272)
06-29 17:37:19.185:W / System.err的(24427):在it.univpm.dii.socialfoot.SocialFootActivity<&初始化GT;(SocialFootActivity.java:56)
06-29 17:37:19.185:W / System.err的(24427):在it.univpm.dii.socialfoot.PostMsg.doInBackground(PostMsg.java:13)
06-29 17:37:19.185:W / System.err的(24427):在it.univpm.dii.socialfoot.PostMsg.doInBackground(PostMsg.java:1)
06-29 17:37:19.185:W / System.err的(24427):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
06-29 17:37:19.185:W / System.err的(24427):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
06-29 17:37:19.185:W / System.err的(24427):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-29 17:37:19.195:W / System.err的(24427):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-29 17:37:19.195:W / System.err的(24427):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
06-29 17:37:19.195:W / System.err的(24427):在java.lang.Thread.run(Thread.java:856)


解决方案

有一个错误在这行你code的:

  SocialFootActivity.this.runOnUiThread(新的Runnable(){
                                    @覆盖
                                    公共无效的run(){
                                新PostMsg()执行(的Hello World);
                                    }});

为什么你想运行一个的AsyncTask 使用 runOnUiThread()

您是假设简单地调用的AsyncTask 在这样的的OnClick()方法:

  OnCheckedChangeListener toggleButtonListener =新OnCheckedChangeListener(){
//调用时用户切换会话状态
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
        布尔器isChecked){
        如果(!器isChecked){
        AlertDialog.Builder建设者=新AlertDialog.Builder(
                SocialFootActivity.this);
        builder.setMessage(
                你想在Facebook上共享)
                .setCancelable(假)
                .setPositiveButton(是,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话框,
                                    INT ID){                                新PostMsg()执行(的Hello World);
                                    });
                            }

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

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 http://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)

解决方案

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.

这篇关于一个对话框,里面的AsyncTask - 内螺纹无法创建处理已经不叫尺蠖prepare()。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆