【问题标题】:Application hangs or collapses on submitting the data in the sign-up form, second time...First time it run perfectly应用程序在注册表单中提交数据时挂起或崩溃,第二次......第一次完美运行
【发布时间】:2024-05-29 18:50:01
【问题描述】:

下面是注册表单的代码,当我们第二次单击以提交输入的详细信息时,应用程序崩溃并仅显示 dialog_thread....在第一次执行时它完美地朗姆酒...... ...... dialog_thread的代码是在onClick()之后给出的......请帮帮我,如何摆脱这个问题,让应用程序顺利运行......

请快帮我...

public void onClick(查看 arg0) { // TODO 自动生成的方法存根

    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());
    }
}

}

(此代码位于不同的文件中)

类 dialog_thread 扩展 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();
}

//**************** 课程到此结束
}

【问题讨论】:

    标签: android performance


    【解决方案1】:

    在 Java 中,您不能重新启动线程。在您的 onClick 中重新创建线程。只需再次调用其构造函数即可。它第一次工作,因为线程还没有停止。在您从处理操作返回第一次编辑后,线程被停止,因此第二次它崩溃,因为您试图重新启动已停止的线程。

    希望对您有所帮助。

    【讨论】:

      最近更新 更多