简体   繁体   English

Android Studio 无法更新公共变量

[英]Android Studio can't update public variable

I am very new to Java and Android Studio.我对 Java 和 Android Studio 很陌生。 I am creating a login script, everything works but I need to set a variable to false or true .我正在创建一个登录脚本,一切正常,但我需要将一个变量设置为falsetrue Java tells me I can't update the variable because I am using a public void . Java 告诉我我无法更新变量,因为我使用的是public void So I am trying to work around that but it's not working at all.所以我试图解决这个问题,但它根本不起作用。 This is my code这是我的代码

public String errorNumber = "0";

That variable is being used in the following method该变量正在以下方法中使用

public boolean validate() {

        boolean valid = true;


        String email = _emailText.getText().toString().trim();
        String password = _passwordText.getText().toString().trim();

        final String Password = _passwordText.getText().toString().trim();
        final String Email = _emailText.getText().toString().trim();

        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            _emailText.setError("Vul een geldige email in");
            valid = false;


        } else {
            _emailText.setError(null);
        }

        if (password.isEmpty() || password.length() < 8 || password.length() > 20) {
            _passwordText.setError("Vul een geldig wachtwoord in");
             valid = false;


        } else {


            StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {

                            String[] output = response.split(":");
                            String output2 = output[2];
                            String[] setNumber = output[1].split(",");
                            String responseDetails1 = output2.replace("\"", "");
                            String responseDetails = responseDetails1.replace("}", "");
                            String responsNumber = setNumber[0];

                            Log.d("RESPONSE1", response);
                            Log.d("RESPONSE2", setNumber[0]);
                            Log.d("RESPONSE3", responseDetails);

                            if(responsNumber.equals("0")){

                                 errorNumber = "0";

                                //Toast.makeText(login.this, responseDetails, Toast.LENGTH_LONG).show();

                                Log.d("THERE IS AN ERROR", responseDetails);
                            }else{

                                _passwordText.setError(null);

                                 errorNumber = "1";

                                //Toast.makeText(login.this,  responseDetails, Toast.LENGTH_LONG).show();

                                Log.d("LOGIN SUCCESS", responseDetails);

                                //Intent i = new Intent(login.this, login.class);

                               // startActivity(i);
                            }


                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(login.this,error.toString(),Toast.LENGTH_LONG).show();
                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<>();
                    params.put(KEY_PASSWORD, Password);
                    params.put(KEY_USEREMAIL, Email);

                    return params;
                }

            };

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

        }
        Log.d("ERRORNUMBER", errorNumber);

        if (errorNumber.equals("0")){
            valid = false;
        }

        Log.v("VALID STATUS", ""+ valid);

        return valid;
    }

Sorry for the long block of code, but it basically updates valid to true or false.抱歉,代码块很长,但它基本上将valid更新为 true 或 false。 Because I can't do this from within my if statements I needed to work around it, so I thought why not setup a variable and later check if that is set to 1 or 0?因为我不能在我的if语句中做到这一点,所以我需要解决它,所以我想为什么不设置一个变量,然后检查它是设置为 1 还是 0? Then update the valid boolean accordingly.然后相应地更新有效的布尔值。

Nothing I have tried so far works and I am out of solutions.到目前为止我尝试过的任何方法都不起作用,而且我没有解决方案。 I have searched the web but I can't find anything that explains me why I can't update that update variable so I can use it in my last if statement.我在网上搜索过,但找不到任何可以解释我为什么无法更新该更新变量的内容,以便我可以在最后的if语句中使用它。

If I'm not mistaken, what you are doing wrong is that you are declaring variables IN the method.如果我没记错的话,你做错的是你在方法中声明了变量。 This results in them being deleted once you reach the end of the method.这会导致一旦您到达方法的末尾,它们就会被删除。 There are multiple ways to solve this, I will give you two ways.有多种方法可以解决这个问题,我将给您两种方法。

  1. You should declare 'valid', 'email' and 'password' variables outside of every method, at the beginning of the class.您应该在类的开头在每个方法之外声明“valid”、“email”和“password”变量。 That way, when you invoke the method, whatever you did with variables is saved.这样,当您调用该方法时,您对变量所做的一切都会被保存。

  2. You could make the validate() method return a variable.您可以使 validate() 方法返回一个变量。 For example, have the method return 'valid' variable and then you will get the value of 'valid' once you exit the method.例如,让方法返回 'valid' 变量,然后一旦退出该方法,您将获得 'valid' 的值。

EDIT: just thought about 3. way you could do this.编辑:只是想过 3. 你可以这样做的方式。 You could make a separate class, make an object out of it in your validate() method and return the whole object.你可以创建一个单独的类,在你的 validate() 方法中创建一个对象并返回整个对象。 That way you can get all 3 variables without declaring them for the whole class.这样您就可以获得所有 3 个变量,而无需为整个类声明它们。

Public class email () {
    Boolean valid;
    String password;
    String email:
}

If this answer is not understandable, ask for further clarification.如果此答案无法理解,请要求进一步说明。 I can also post example code if needed.如果需要,我还可以发布示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM