新的 View.OnClickListener() 按钮使整个应用程序崩溃

new View.OnClickListener() Button Crashes the whole application

我的应用程序一切正常,今天早上当我尝试 运行 它并按下“注册”按钮时,应用程序崩溃了。 代码中的另一个按钮也是如此。 在 IDE(Android Studio)里面写着

btnRegister.setOnClickListener(new View.OnClickListener()

不确定我添加或收费的内容,但这是我的代码。 不确定我是否应该添加所有代码,因为它太长了。

XML 文件

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toBottomOf="@+id/cbAgreement" />

这是我的 Java 代码

btnRegister = (Button) findViewById(R.id.btnRegister);


        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initRegister();
            }
        });

private void initRegister(){
        Log.d(TAG, "initRegister: Started");
        if (validateData())
        {
            if (cbAgreement.isChecked())
            {
                showSnackBar();
            }else{
                Toast.makeText(this, "You need to agree to the License Agreement", Toast.LENGTH_SHORT).show();
            }
        }
    }

private void showSnackBar(){
        Log.d(TAG, "showSnackBar: Started");

        txtFullName.setVisibility(View.GONE);
        txtEmail.setVisibility(View.GONE);
        txtPassword.setVisibility(View.GONE);
        edtTxtRePassword.setVisibility(View.GONE);

        String name = edtTxtFullName.getText().toString();
        String email = edtTxtEmail.getText().toString();
        String country = countrySpinner.getSelectedItem().toString();
        String gender = "";

        switch (radioGroupGender.getCheckedRadioButtonId())
        {
            case R.id.rbMale:
                gender = "Male";
                break;
            case R.id.rbFemale:
                gender = "Female";
                break;
            case R.id.rbOther:
                gender = "Other";
                break;
            default:
                gender = "Unknown";
                break;
        }

        String snackText =
                "Name " + name + "\n" +
                "Email " + email + "\n" +
                "Gender " + gender + "\n" +
                "Country " + country + "\n";

        Snackbar.make(parent, name + " Registered!", Snackbar.LENGTH_INDEFINITE)
                .setAction("Dismissed", view -> {
                    txtFullName.setText("");
                    txtEmail.setText("");
                    txtPassword.setText("");
                    txtRePassword.setText("");
                }).show();

    }



Logcat

2021-06-17 18:27:06.107 10598-10598/com.example.registerform D/MainActivity: validateData: Started
2021-06-17 18:27:06.107 10598-10598/com.example.registerform E/le.registerfor: Invalid ID 0x00000000.
2021-06-17 18:27:06.107 10598-10598/com.example.registerform D/AndroidRuntime: Shutting down VM
2021-06-17 18:27:06.113 10598-10598/com.example.registerform E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.registerform, PID: 10598
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:381)
        at android.content.res.MiuiResources.getText(MiuiResources.java:97)
        at android.widget.TextView.setText(TextView.java:6397)
        at com.example.registerform.MainActivity.validateData(MainActivity.java:118)
        at com.example.registerform.MainActivity.initRegister(MainActivity.java:57)
        at com.example.registerform.MainActivity.access[=14=]0(MainActivity.java:19)
        at com.example.registerform.MainActivity.onClick(MainActivity.java:49)
        at android.view.View.performClick(View.java:7183)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7156)
        at android.view.View.access00(View.java:820)
        at android.view.View$PerformClick.run(View.java:27650)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7561)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
2021-06-17 18:27:06.128 10598-10598/com.example.registerform D/OOMEventManagerFK: checkEventAndDumpForJE: 0
2021-06-17 18:27:06.164 10598-10598/com.example.registerform I/Process: Sending signal. PID: 10598 SIG: 9
    private boolean validateData(){
        Log.d(TAG, "validateData: Started");

        if(edtTxtFullName.getText().toString().equals(""))
        {
            txtFullName.setText(View.VISIBLE);
            return false;
        }
        if(edtTxtEmail.getText().toString().equals(""))
        {
            txtEmail.setText(View.VISIBLE);
            return false;
        }
        if(edtTxtPassword.getText().toString().equals(""))
        {
            txtPassword.setText(View.VISIBLE);
            return false;
        }
        if(edtTxtRePassword.getText().toString().equals(""))
        {
            txtRePassword.setText(View.VISIBLE);
            return false;
        }
        if(!edtTxtPassword.getText().toString().equals(edtTxtRePassword.getText().toString())){
            Toast.makeText(this, "Password Doesn't Match.", Toast.LENGTH_SHORT).show();
            txtRePassword.setText(View.VISIBLE);
            return false;
        }

        return true;
    }
    private void initViews(){
        Log.d(TAG,"Started");

        edtTxtFullName = findViewById(R.id.edtTxtFullName);
        edtTxtEmail = findViewById(R.id.edtTxtEmail);
        edtTxtPassword = findViewById(R.id.edtTxtPassword);
        edtTxtRePassword = findViewById(R.id.edtTxtRePassword);

        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnImage =(Button) findViewById(R.id.btnImage);

        txtFullName = findViewById(R.id.txtFullName);
        txtEmail = findViewById(R.id.txtEmail);
        txtPassword = findViewById(R.id.txtPassword);
        txtRePassword = findViewById(R.id.txtRePassword);

        countrySpinner = findViewById(R.id.countrySpinner);
        radioGroupGender = findViewById(R.id.radioGroupGender);
        cbAgreement = findViewById(R.id.cbAgreement);
        parent = findViewById(R.id.parent);


    }

您的应用程序崩溃是因为这个(在 validateData() 中)函数:

txtFullName.setText(View.VISIBLE);

和其他类似的行。 TextViewsetText() 函数应该与 StringCharSequence 等一起使用,所以这是设置可见性的错误函数,这就是我假设的正在努力做。

改为使用:

txtFullName.setVisibility(View.VISIBLE);

这应该使 View 可见。

将来,使用 Logcat 打印的堆栈跟踪和 Android Studio 提供的调试工具将使跟踪和修复错误变得更加容易。