0

データベース テーブルに行を追加するときに進行状況ダイアログを表示しようとしていますが、進行状況ダイアログが 1 秒間しか表示されず、その後アプリケーションがクラッシュするという問題があります。

public void onClick(View arg0) {
        // TODO Auto-generated method stub

        ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) 
        {
             final EditText wholepost=(EditText)findViewById(R.id.wholepost);
                final String post_type=spinner.getSelectedItem().toString();
                final String post_type2=post_type.replace(" ", "%20");
                final String post1=wholepost.getText().toString();
                String post2=post1.replace(" ", "%20");
                final String post3=post2.replace("\n","%0D");
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                final String now = df.format(new Date());

                if(post1.equals(null))
                {
                    Toast.makeText(getBaseContext(), "Please write a post before clicking on the button",Toast.LENGTH_LONG).show();
                }
                else
                {
                    if(post1.length()<15)
                    {
                        Toast.makeText(getBaseContext(), "Post is too small",Toast.LENGTH_LONG).show();
                    }

                    else
                    {
                        if(post1.length()>500)
                        {Toast.makeText(getBaseContext(), "Post should be smaller",Toast.LENGTH_LONG).show();}
                        else
                        {
                            pd.show();

                            Thread th=new Thread(new Runnable() {
                                public void run() {                             

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://192.168.1.38/test/Addpost.php?username="+username+"&fname="+firstname+"&lname="+lastname+"&dop="+now+"&content="+post3+"&type="+post_type2+"");

                        try
                        {

                            nameValuePairs = new ArrayList<NameValuePair>();
                            nameValuePairs.add(new BasicNameValuePair("FirstN",firstname));
                            nameValuePairs.add(new BasicNameValuePair("LastN",lastname));
                            nameValuePairs.add(new BasicNameValuePair("Content",post1));
                            nameValuePairs.add(new BasicNameValuePair("type",post_type));
                            nameValuePairs.add(new BasicNameValuePair("Dateofp",now));
                            nameValuePairs.add(new BasicNameValuePair("username",username));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            response = httpclient.execute(httppost);

                            if(response.getStatusLine().getStatusCode()==200)
                            {
                            entity=response.getEntity();
                            if(entity !=null)
                            {
                                Toast.makeText(getBaseContext(),"POST SUCCESFULLY ADDED",Toast.LENGTH_LONG).show(); 
                            }

                            }
                            else
                            {
                                Toast.makeText(getBaseContext(),"ERROR RERTY OR CHECK YOUR CONNECTION",Toast.LENGTH_LONG).show();
                            }





                            wholepost.setText("");





                        }
                        catch(Exception ex)
                        {Toast.makeText(getBaseContext(),"CONNECTION ERROR",Toast.LENGTH_LONG).show();}


                        pd.dismiss();
                                }   
                            });
                            th.start();


                    }}
                }

        }
        else
        {
            Toast.makeText(getBaseContext(),"Please make sure you're connected to the internet",Toast.LENGTH_LONG).show();

        }




    }
});
4

1 に答える 1

0

Asyntaskを使用するか、そうでなければスレッドをスリープさせます

于 2013-03-29T12:17:36.740 に答える