猿问

调试时 onPostExecute() 与 doInBackground() 无关

我onPostExecute()在AsyncTask课堂上的方法有问题。


我有一个SignupActivity:


public class SignupActivity extends AppCompatActivity implements SignupListener {


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.signup_activity);


    //new task, i pass context and interface to it

    signup = new Signup(getApplicationContext(), this);


    signupButon.setOnClickListener(new View.OnClickListener(){

        @Override

        public void onClick(View v){

            if(validate()) {

                try {

                    //new task every click

                    Signup newSignup = new Signup(signup);

                    //here start AsyncTask

                    newSignup.execute(name, email, password).get();

                } catch (Exception e) {

                    Toast.makeText(ERROR);

                }

                // if sign up succes, == true;

                if(signupValid) {

                    Toast.makeText(SUCCES);

                    finish();

                } else {

                    Toast.makeText(ERROR);

                }

            }

        }

    });

}


// my own interface for getting result as bool from onPostExecute

@Override

public void onSignupPerformed(Boolean result){ this.signupValid = result; }

这实现了我从以下位置捕获结果的接口onPostExecute():


public interface SignupListener{

    void onSignupPerformed(Boolean result);

}

现在,AsyncTask我在代码中触发:


public class Signup extends AsyncTask<String,  Boolean, Boolean> {


public Signup(Context context, SignupListener listener){

    db = ApplicationDatabase.getDatabase(context);

    this.context = context;

    this.listener = listener;

}


public Signup(Signup signup){

    //constructor to make new task based on first task

    db = signup.db;

    context = signup.context;

    listener = signup.listener;

}


我的问题是,为什么当我第一次单击按钮时,func 返回Boolean.TRUE但在SignupActivity signupValid变量中是false(注册表单未退出,但用户已添加到 DB),但是当我第二次单击注册按钮时,ofc 注册失败(因为我们创建了新用户几秒钟前)但signupValid更改为true并通过注册表单?我需要单击SignupButton两次才能最终退出表单。感谢您在我的代码中发现错误


森栏
浏览 76回答 1
1回答

牧羊人nacy

如果我正确理解了这个问题 - 有一个竞争条件使得在任务SignupActivity执行完成之前触发 toast Signup。所以:signupButon.setOnClickListener(new View.OnClickListener(){&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v){&nbsp; &nbsp; &nbsp; &nbsp; if(validate()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //new task every click&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Signup newSignup = new Signup(signup);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //here start AsyncTask&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSignup.execute(name, email, password).get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(ERROR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});虽然这些行:&nbsp;// if sign up succes, == true;&nbsp;if(signupValid) {&nbsp; &nbsp; Toast.makeText(SUCCES);&nbsp; &nbsp; finish();&nbsp;} else {&nbsp; &nbsp; Toast.makeText(ERROR);&nbsp;}应该是其中的一部分listener(现在看来这些行是在完成异步任务之前执行的)为了澄清我自己:@Overridepublic void onSignupPerformed(Boolean result){&nbsp;&nbsp; &nbsp; &nbsp;if(result) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(SUCCES);&nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(ERROR);&nbsp; &nbsp; &nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答