我相信按钮约束问题

问题描述

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

java代码

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>

任何帮助将不胜感激

解决方法

好像有一些编码格式错误。用这个替换你上面的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();
    }
});
,

这本身不是错误,而是编译时警告,有时警告无关紧要,有时则不然。

另一种非解决方案是使用

抑制该警告
@SuppressWarnings ("unchecked") 

@Suppress("unchecked")

之前

btnSignin = findViewById(R.id.Sign_In); // remove (Button),is not necesary

抑制警告

@SuppressWarnings 指示编译器忽略或抑制 在带注释的元素和所有程序中指定编译器警告 该元素内的元素,例如,如果一个类被注释 抑制特定警告,然后在此期间生成警告 该类中的方法论也将被压制。

@SuppressWarnings("unchecked")@SuppressWarnings("serial") 是@SuppressWarnings 注释的两个最受欢迎的示例。还 如果你限制范围,这样它甚至不会影响整个 方法。

SuppressWarnings 表示潜在的编程错误或错误, 所以建议细细地使用它们,而不是尝试解决 触发该警告的实际问题。但是这里有 当你完全确定事情不会出错时, 您可以使用 @SuppressWarnings 来抑制这些警告。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...