views:

14

answers:

1

Hi,

below is the code of a sign up form, when we CLICK second time to submit the entered details, the application collapses and hags just displaying the dialog_thread....On executing very first time it rum perfectly..............The code of dialog_thread is given after onClick()....Please help me, how to get rid of this problem and let the application run smoothly.....

PLZ HELP ME FAST...

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

    alert_dialog ad=new alert_dialog();

    try
    {
        if(arg0==signup)
        {
            if(password.getText().toString().equals(confirm_password.getText().toString()))
            {
                //**** Start loading dialog thread

                dt=new dialog_thread(this);
                Log.i("dialog thread","Object created");

                dt.start();
                Log.i("dialog thread","thread started");

                String message=null;
                Integer success=null;

                //************ Encrypt the password
                String encrypt_password=new md5(password.getText().toString()).getEncryped();

                //************* create name value pair
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password",encrypt_password));
                nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("func","signup"));
                nameValuePairs.add(new BasicNameValuePair("user_type",s.getSelectedItem().toString()));

                DBConnect db=new DBConnect();
                String result=db.fetch_data(nameValuePairs);
                //******** Toast.makeText(this, result, Toast.LENGTH_LONG).show();

                //************* Parse JSONArray returned

                JSONArray jArray = new JSONArray(result);

                for(int i=0;i<jArray.length();i++)
                {
                    JSONObject json_data = jArray.getJSONObject(i);                 
                    success=json_data.getInt("success");                    
                    message=json_data.getString("message");                 
                }

                //************ Stop loading dialog thread
                dt.stop_execution();

                if(success==1)
                    Toast.makeText(this,"You have sucessfully signed up", Toast.LENGTH_LONG).show();
                else
                    {                       
                        ad.alertbox(this, "", message);
                    }
            }
            else
                ad.alertbox(this, "", "Error : Please enter the same password");
        }
    }catch(Exception e)
    {
        Log.i("Exception in signup",e.toString());
    }
}

}

(THIS CODE IS IN A DIFFERENT FILE)

class dialog_thread extends Thread {

ProgressDialog pd;
Context c;
String title="Working ...";
String msg="Please Wait ...";

dialog_thread(Context cont)
{
    c=cont;
}

dialog_thread(Context cont,String title,String msg)
{
    c=cont;
    this.title=title;
    this.msg=msg;
}

public void run() 
{
    // TODO Auto-generated method stub
  try
    {
        Looper.prepare();
        pd = ProgressDialog.show(c, this.title, this.msg, true, false);
        Handler handler = new Handler() 
                                {               
                                    public void handleMessage(Message msg) 
                                        {
                                            //pd.dismiss();                                          
                                            Log.i("Handler","Inside handler");
                                            Log.i("dissmissing thread",msg.toString());
                                        }               
                                };
        handler.sendEmptyMessage(0);
        Log.i("TRy block","Executed");
        Looper.loop();  
    }
    catch(Exception e)
    {
        Log.i("Exception in dialog thread",e.toString());
    }
//************************ Run ends here    
}

public void stop_execution()
{
    pd.dismiss();
    this.stop();
}

//******** Class ends here
}

A: 

In Java you cant restart a thread. In your onClick recreate the thread. Simply calling its constructor again will work. It works the first time because the thread hasn't been stopped yet. After the first edit when you return from handling the action the thread is stopped and so the second time around it crashes because you are trying to restart a stopped thread.

Hope that helps.

Mike