我正在使用AsyncTask并出现应用程序可能在其主线程上做了太多工作

时间:2014-07-31 21:09:35

标签: android android-asynctask

我有这个代码,它将一个变量发送到webservice并返回一个答案。

我使用AsyncTask进行此过程。

在流程结束时,OnPostExecute函数打开一个包含webview的AlertDialog并向我显示一个url。

直到那里一切都很好。

详细信息是LogCat向我展示:应用程序可能在其主线程上做了太多工作。

我不知道为什么会发生这种情况,如果我正在使用AsyncTask进行除main之外的线程的进程。

有人可以帮帮我吗?

谢谢

类别:

public class Donar extends Fragment {

    ImageButton insertar;
    String emailAdd;
    String name;
    ImageView x;
    ProgressDialog pd;



    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {

        View x = inflater.inflate(R.layout.donar, container, false);


        // Capturo las variables que dejo en memoria, nombre y email

        SharedPreferences sp1 = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
        emailAdd = sp1.getString("EMAILADDRESS", "");
        name = sp1.getString("NAME", "");

        //-----------------------------------------------

        insertar=(ImageButton) x.findViewById(R.id.imageButton1);   


        insertar.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                    new Insertar(getActivity()).execute();      

            }

        });


        return x;
    }



//Inserta los datos de las Personas en el servidor.
        private String insertar(){  

            // Declaro variables

            String response = "";
            HttpClient httpclient;
            List<NameValuePair> nameValuePairs;
            HttpPost httppost;

            //-------------------------

            httpclient=new DefaultHttpClient(); // abre la conexion con un cliente web

            httppost= new HttpPost("http:xxxxx/insert.php"); // Url del Servidor a donde se envian las var post

            //Añadimos nuestros datos en un arraylist

            nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("email",emailAdd.toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("nombre",name.toString().trim()));

            //---------------------------------------


            try {
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));// envio las variables

               // capturo lo que devuelve, si no devolviera nada solo hago la ejecucion del httpclient sin el handler

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

               //----------------------------------------------------------------

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return response;
        }       

        //AsyncTask para insertar Personas
        class Insertar extends AsyncTask<String,String,String>{

            private Activity context;

            Insertar(Activity context){
                this.context=context;
            }
            @Override
            protected String doInBackground(String... params) {
              // TODO Auto-generated method stub


              String result = insertar();

              return result;
            }


            @Override
            protected void onPostExecute(String result) {


                AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
                alert.setTitle("Dona1Click - Patrocinador");




                WebView wv = new WebView(getActivity());
                wv.loadUrl("http:\\www.google.com");
                wv.setWebViewClient(new WebViewClient() {

                    public void onPageFinished(WebView view, String url) {
                        if(pd.isShowing()&&pd!=null)
                        {
                            pd.dismiss();
                        }
                    }


                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);

                        return false;
                    }
                });

                alert.setView(wv);
                alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
                alert.show();

             // Para colocar un loading

                pd = ProgressDialog.show(getActivity(), "", "Loading...",true);

                //--------------------------


            }

        }

}

日志:

07-31 16:56:51.136: I/Choreographer(1100): Skipped 37 frames!  The application may be doing too much work on its main thread.
07-31 16:56:51.536: I/Choreographer(1100): Skipped 99 frames!  The application may be doing too much work on its main thread.
07-31 16:56:52.216: I/Choreographer(1100): Skipped 61 frames!  The application may be doing too much work on its main thread.
07-31 16:56:52.666: I/Choreographer(1100): Skipped 48 frames!  The application may be doing too much work on its main thread.
07-31 16:56:52.896: I/Choreographer(1100): Skipped 57 frames!  The application may be doing too much work on its main thread.
07-31 16:56:53.046: I/Choreographer(1100): Skipped 39 frames!  The application may be doing too much work on its main thread.
07-31 16:56:54.286: I/Choreographer(1100): Skipped 45 frames!  The application may be doing too much work on its main thread.
07-31 16:56:54.416: I/Choreographer(1100): Skipped 32 frames!  The application may be doing too much work on its main thread.
07-31 16:56:54.556: I/Choreographer(1100): Skipped 35 frames!  The application may be doing too much work on its main thread.
07-31 16:56:56.286: I/Choreographer(1100): Skipped 30 frames!  The application may be doing too much work on its main thread.
07-31 16:56:57.006: I/Choreographer(1100): Skipped 98 frames!  The application may be doing too much work on its main thread.
07-31 16:56:57.616: I/Choreographer(1100): Skipped 36 frames!  The application may be doing too much work on its main thread.

0 个答案:

没有答案