不能内螺纹创建处理程序,也没有所谓的活套。在android系统prepare() [英] Can't create handler inside thread that has not called Looper.prepare() in android

查看:160
本文介绍了不能内螺纹创建处理程序,也没有所谓的活套。在android系统prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我越来越不能创建在异步后台任务内螺纹处理程序。下面是我的code。我做了必要的修改在谷歌搜索,但仍然错误上升后,进度条。请帮我这个。将感谢。

我的code:

 私有类LongOperation1扩展的AsyncTask<弦乐,太虚,字符串> {        私人最终的WeakReference< MainActivity> mainActivityWeakRef;
        ProgressDialog对话框;        公共LongOperation1(MainActivity mainActivity){
            超();
            this.mainActivityWeakRef =新的WeakReference< MainActivity>(
                    主要活动);
            // this.activity = mainActivity;
        }        @覆盖
        保护字符串doInBackground(字符串... PARAMS){            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(
                    的http://.............php);            //这是要发送的数据
            //字符串MYNAME ='阿迪尔'; //任何数据发送
            // publishProgress(5);            尝试{
                //添加数据
                清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(
                        1);
                nameValuePairs.add(新BasicNameValuePair(参数1,1));
                nameValuePairs.add(新BasicNameValuePair(参数2
                        城市));
                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));                //执行HTTP POST请求                ResponseHandler所<串GT; ResponseHandler所=新BasicResponseHandler();
                字符串的响应= httpclient.execute(httppost,ResponseHandler所);                //这是一个PHP应用程序的响应
                字符串reverseString =响应;
                Toast.makeText(MainActivity.this,回应+ reverseString,
                        Toast.LENGTH_LONG).show();            }赶上(ClientProtocolException E){
                Toast.makeText(MainActivity.this,
                        CPE响应+ e.toString(),Toast.LENGTH_LONG)
                        。显示();
                // TODO自动生成catch块
            }赶上(IOException异常五){
                Toast.makeText(MainActivity.this,
                        IOE响应+ e.toString(),Toast.LENGTH_LONG)
                        。显示();
                // TODO自动生成catch块
            }
            返回全部完成!        }        @覆盖
        保护无效onPostExecute(字符串结果){
            Log.d(onpostexecute(mainActivityWeakRef.get()= NULL)+!);
            如果(mainActivityWeakRef.get()!=空
                    &功放;&安培; !mainActivityWeakRef.get()。isFinishing()){                AlertDialog alertDialog =新AlertDialog.Builder(
                        。mainActivityWeakRef.get())创建();
                alertDialog.setTitle(结果);
                alertDialog.setMessage(关于执行后);
                alertDialog.setCancelable(假);
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,OK,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话框,
                                    其中INT){
                                dialog.dismiss();
                            }
                        });                alertDialog.show();
            }        }        @覆盖
        保护无效onProgressUpdate(虚空......值){
            super.onProgressUpdate(值);
            dialog.incrementProgressBy(5);
        }
    }


解决方案

由于您所需要的UIThread向他们展示你不能使用面包在后台操作的事情。 Instean这样做,因为你做的。使用一个变量的保存它为不同的状态。

在onPostExecute检查变量,并显示相应的吐司。

希望它帮助。

I am getting can't create handler inside thread in asynchronous background task. Below is my code. I made the necessary modifications to progress bar after searching in google but still the error is rising. Please help me with this. Will be thankful.

My code:

private class LongOperation1 extends AsyncTask<String, Void, String> {

        private final WeakReference<MainActivity> mainActivityWeakRef;
        ProgressDialog dialog;

        public LongOperation1(MainActivity mainActivity) {
            super();
            this.mainActivityWeakRef = new WeakReference<MainActivity>(
                    mainActivity);
            // this.activity = mainActivity;
        }

        @Override
        protected String doInBackground(String... params) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://.............php");

            // This is the data to send
            // String MyName = 'adil'; //any data to send
            // publishProgress(5);

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        1);
                nameValuePairs.add(new BasicNameValuePair("param1", "1"));
                nameValuePairs.add(new BasicNameValuePair("param2",
                        "Cities"));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request

                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httppost, responseHandler);

                // This is the response from a php application
                String reverseString = response;
                Toast.makeText(MainActivity.this, "response" + reverseString,
                        Toast.LENGTH_LONG).show();

            } catch (ClientProtocolException e) {
                Toast.makeText(MainActivity.this,
                        "CPE response " + e.toString(), Toast.LENGTH_LONG)
                        .show();
                // TODO Auto-generated catch block
            } catch (IOException e) {
                Toast.makeText(MainActivity.this,
                        "IOE response " + e.toString(), Toast.LENGTH_LONG)
                        .show();
                // TODO Auto-generated catch block
            }
            return "All Done!";

        }

        @Override
        protected void onPostExecute(String result) {
            Log.d("onpostexecute", (mainActivityWeakRef.get() != null) + "");
            if (mainActivityWeakRef.get() != null
                    && !mainActivityWeakRef.get().isFinishing()) {

                AlertDialog alertDialog = new AlertDialog.Builder(
                        mainActivityWeakRef.get()).create();
                alertDialog.setTitle(result);
                alertDialog.setMessage("On post execute");
                alertDialog.setCancelable(false);
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });

                alertDialog.show();
            }

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            dialog.incrementProgressBy(5);
        }
    }

解决方案

You cannot use Toast in the do in background operation because you need the UIThread to show them. Instean of doing that as you did. Use a variable an save it for different states.

In onPostExecute check the variable and show the corresponding Toast.

Hope it helps.

这篇关于不能内螺纹创建处理程序,也没有所谓的活套。在android系统prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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