1

这是我的代码的一部分:单击按钮后,将启动新线程和与服务器的连接。如果连接成功,应用程序应该开始新的活动并结束当前活动。有人可以解释哪种方式最适合这样做吗?

    transThread.submit(new Runnable()
            {
                public void run()
                {
                    guiProgressDialog(true);
                    if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
                    else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
                    else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
                    else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
                    else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
                    else {
                        try {
                            final String message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
                            guiNotifyUser(message);
//if message equals something start new activity

                        }
                        catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                    guiProgressDialog(false);
                }
            });

            break;
4

2 回答 2

3

您可以为此使用runOnUiThread这是一个 SO answer,显示了如何做到这一点。

我个人喜欢用AsyncTask这个。您可以在其中完成您的工作,doInBackground()然后返回一个值onPostExecute()Activity从那里开始,或者在UI.

AsyncTask 文档

这是我的一个答案,它显示了使用的基本结构和重要细节AsyncTask

从注释中的代码编辑

我不能说没有 logcat 的确切错误,但我看到的第一个问题是当你contextAsyncTask. 你不想使用,getApplicationContext()尤其不是你现在的样子。我想你会得到一个NPE因为context尚未初始化。您正在传入Context构造函数,所以您只需执行

this.context = context

但是,看起来您AsyncTask是一个内部类,RegisterActivity这意味着它可以访问所有成员变量RegisterActivity及其Context. 这意味着要启动您Activity可以使用RegisterActivity.this而不是context.

    @Override
    protected void onPostExecute(String result)
    {                     
          super.onPostExecute(result);                       
          //if (result == "Successful registration!")                    
          //String i;
          //i = "da";
          context.startActivity(new Intent(RegisterActivity.this,LoginActivity.class));  // change this here

如前所述,如果它是一个内部类,则不需要您的构造函数,context但如果它是一个单独的文件,它会像

class RegisterTask extends AsyncTask<String, Void, String>{

            Context context;
            private RegisterTask(Context context){
                    this.context = context;  // use the variable (context) passed in the constructor above
于 2013-06-20T16:41:49.553 回答
0

这是解决我问题的代码(感谢 CodeMagic - 我不确定这是否是最好的方法): 内部类:

class RegisterTask extends AsyncTask<String, Void, String>{


    @Override
    protected String doInBackground(String... params) {
        String message = null;
        EditText username = (EditText) findViewById( R.id.register_username_text );
        EditText password = (EditText) findViewById( R.id.register_password_text );
        EditText passwordrtp = (EditText) findViewById( R.id.register_repeatpassword_text );
        EditText email = (EditText) findViewById( R.id.register_email_text );
        EditText firstname = (EditText) findViewById( R.id.register_firstname_text );
        EditText lastname = (EditText) findViewById( R.id.register_lastname_text );
        final String user = username.getText().toString();
        final String pass = password.getText().toString();
        final String passrtp = passwordrtp.getText().toString();
        final String mail = email.getText().toString();
        final String fname = firstname.getText().toString();
        final String lname = lastname.getText().toString();

        guiProgressDialog(true);
        if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
        else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
        else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
        else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
        else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
        else {
            try {
                message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
                guiNotifyUser(message);//prosla vodi na sledeci activity "Succesfull registration"

            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        guiProgressDialog(false);



        return message;
    }

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);

        if (result.equalsIgnoreCase("Successful registration!"))
        {

            RegisterActivity.this.startActivity(new Intent(context,LoginActivity.class));
            RegisterActivity.this.finish();
        }


    }
}

并从按钮监听器调用:

public void onClick(View v) {

    switch(v.getId())
    {
    case R.id.register_register_button:

        RegisterTask registerTask = new RegisterTask();
        registerTask.execute();

        break;
于 2013-06-23T17:12:56.233 回答