首页 > 解决方案 > 我相信按钮约束问题

问题描述

希望周末一切顺利。我正在处理登录和注册页面,很难找到解决未经检查的强制转换错误的方法,经过检查,xml 代码似乎一切正常。挑战真的很令人沮丧,这是代码

爪哇代码

public class RegistrationPage < Button > extends AppCompatActivity {
    EditText emailId, password;
    Button btnSignin;
    TextView tvSign_Up;
    FirebaseAuth mFirebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_registration_page);

            mFirebaseAuth = FirebaseAuth.getInstance();
            emailId = findViewById(R.id.editTextTextEmailAddress);
            password = findViewById(R.id.editTextTextPassword);
            btnSignin = (Button) findViewById(R.id.Sign_In);
            tvSign_Up = (findViewById(R.id.textView2));
            btnSignin.(new RegistrationPage < > () View.OnClickListener(("v"))) - > {
                    @Override
                    public void onClick(View "V") {
                        string email = emailId.getText().toString();
                        string pwd = password.getText().toString();
                        if (((String) email).isEmpty()) {
                            emailId.setError("Please Enter Correct Email Address");
                            emailId.requestFocus();
                        }

xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".RegistrationPage">

<EditText
android:id="@+id/editTextTextEmailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginLeft="84dp"
android:layout_marginTop="152dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="313dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress"
app:layout_constraintVertical_bias="0.092" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:layout_marginBottom="200dp"
android:text="@string/already_have_an_account_sign_in_here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.677" />
<Button
android:id="@+id/Sign_In"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:text="@string/sign_up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.054" />

</androidx.constraintlayout.widget.ConstraintLayout>

任何帮助将不胜感激

标签: androidandroid-studioconstraintsunchecked-cast

解决方案


似乎有一些编码格式错误。用这个替换你上面的java代码并尝试一下:

mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editTextTextEmailAddress);
password = findViewById(R.id.editTextTextPassword);
btnSignin = findViewById(R.id.Sign_In);
tvSign_Up = findViewById(R.id.textView2);

btnSignin.setOnClickListener(v -> {
    String email = emailId.getText().toString();
    String pwd = password.getText().toString();
    if (email.isEmpty()) {
        emailId.setError("Please Enter Correct Email Address");
        emailId.requestFocus();
    }
});

推荐阅读