为什么在Activity中找不到ViewById()会返回用户输入的值?

时间:2018-03-19 11:50:58

标签: android android-fragments android-activity android-fragmentactivity

我正在创建一个Android应用,其中的方案是用户注册,然后点击"注册"按钮,应用程序将数据发送到REST API。

activity_sign_up_login.java

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/signUp"
        android:text="@string/sign_up"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/logIn"
        android:text="@string/log_in"
        app:layout_constraintStart_toEndOf="@+id/signUp" />

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/mobNum" />

    <EditText
        android:id="@+id/mobNum"
        app:layout_constraintTop_toBottomOf="@+id/countryCode" />


</android.support.constraint.ConstraintLayout>

SignUp.java

    public class SignUpLogIn extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final ConnectionStatus connectionStatus = new ConnectionStatus(getApplicationContext());
            if(!connectionStatus.isOnline()){
                connectionStatus.displayMobileDataSettingsDialog(this, getApplicationContext());
            }
            setContentView(R.layout.activity_sign_up_log_in);


            final TextView mobNumberTextView = (TextView) findViewById(R.id.mobNum);
            final TextView passwordTextView = (TextView) findViewById(R.id.password);
            Button signUp = (Button) findViewById(R.id.signUp);


            final String mobNumber = mobNumberTextView.getText().toString();
            final String password = passwordTextView.getText().toString();

            signUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!validateInputFields(mobNumber, password)){
                        Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                        checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                        checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                        checkSignUpStatusIntent.putExtra("confirmPassword", password);
                        startService(checkSignUpStatusIntent);
                        connectionStatus.showProgress();
                    }
                }
            });
        }

private boolean validateInputFields(String mobNum, String password){
        StringBuffer errors = new StringBuffer();
        int count = 0;
        boolean hasError = false;
        if(mobNum == null || mobNum.isEmpty()){
            ++count;
            errors.append(count + ") Please enter your mobile number");
            errors.append('\n');
            hasError = true;
        }
        if(password == null || password.isEmpty()){
            //cEmail field is empty
            ++count;
            errors.append(count + ") Please enter a password");
            errors.append('\n');
            hasError = true;
        }
        if(hasError){
            Toast.makeText(this, errors.toString(), Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }

在onClick()内部,尽管用户输入了值,但值仍为空。

  1. 为什么?
  2. 如何解决?
  3. 请回答内联。

    PS:

    我无法删除.setOnClickListener

    上方的textView变量的final

    enter image description here

4 个答案:

答案 0 :(得分:0)

您需要从mobNumber方法中获取passwordonClick()

    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    String mobNumber = mobNumberTextView.getText().toString();
    String password = passwordTextView.getText().toString();

            if(!validateInputFields(mobNumber, password)){
                Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                checkSignUpStatusIntent.putExtra("confirmPassword", password);
                startService(checkSignUpStatusIntent);
                connectionStatus.showProgress();
            }
        }
    });

修改

如果您正在使用这些

,则无需将变量声明为final
  TextView mobNumberTextView = (TextView) findViewById(R.id.mobNum);
  TextView passwordTextView = (TextView) findViewById(R.id.password);

onClick()

String mobNumber = mobNumberTextView.getText().toString();
String password = passwordTextView.getText().toString();

编辑2

我刚发现你的问题。

在你的validateInputFields方法中,你传递了2个字符串,mobNum和密码,你在if语句中尝试将2个字符串传递给方法,那就是if(!validateInputFields(mobNumber, password))...但是,你正在尝试的两个字符串通过该方法的时间超出了onClick()的范围,因此,无法访问这些值,而validateInputFields()方法无法正常运行。

编辑3

我只是看到你的xml中有这2个editTexts

 <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/mobNum" />

    <EditText
        android:id="@+id/mobNum"
        app:layout_constraintTop_toBottomOf="@+id/countryCode" />

但您的参考说明了两个textViews

 TextView mobNumberTextView = (TextView) findViewById(R.id.mobNum);
 TextView passwordTextView = (TextView) findViewById(R.id.password);

您应该将两个textView添加到xml,或将TextView mobNumb....更改为

EditText mobNumber...

喜欢这个

 EditText mobNumberTextView = (EditText) findViewById(R.id.mobNum);
 EditText passwordTextView = (EditText) findViewById(R.id.password);

答案 1 :(得分:0)

你必须在onClick之后获得价值。

为什么?

findViewById用于绑定您的观看,绑定后立即调用getText,以及它为何为空。

用此

替换您的代码
    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String mobNumber = mobNumberTextView.getText().toString();
            String password = passwordTextView.getText().toString();

            if(!validateInputFields(mobNumber, password)){
                Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                checkSignUpStatusIntent.putExtra("confirmPassword", password);
                startService(checkSignUpStatusIntent);
                connectionStatus.showProgress();
            }
        }
    });

答案 2 :(得分:0)

请从onClickListener

中的EditText获取值
    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String mobNumber = mobNumberTextView.getText().toString();
            final String password = passwordTextView.getText().toString();
            if(!validateInputFields(mobNumber, password)){
                Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                checkSignUpStatusIntent.putExtra("confirmPassword", password);
                startService(checkSignUpStatusIntent);
                connectionStatus.showProgress();
            }
        }
    });

答案 3 :(得分:0)

您需要从 edittext

中的signUp.setOnClickListener获取价值
    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             String mobNumber = mobNumberTextView.getText().toString();
             String password = passwordTextView.getText().toString();

            if(!validateInputFields(mobNumber, password)){
                Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                checkSignUpStatusIntent.putExtra("confirmPassword", password);
                startService(checkSignUpStatusIntent);
                connectionStatus.showProgress();
            }
        }
    });