How to fix androidx.appcompat.widget.AppCompatButton cannot be cast to com.rey.material.widget.Button

余生长醉 提交于 2020-03-26 01:00:54

问题


I'm building a amazon like app. after coding the loginactivity I can't access it from mainactivity because it always crashes

this is my loginactivity.java

public class loginActivity extends AppCompatActivity
{
    private EditText InputPhoneNumber, InputPassword;
    private Button LoginButtonPage;
    private ProgressDialog loadingBar;

    private String parentDbname = "Users";

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

        LoginButtonPage = findViewById(R.id.page_login_btn);
        InputPhoneNumber = findViewById(R.id.login_phone_number_input);
        InputPassword = findViewById(R.id.login_password_input);
        loadingBar = new ProgressDialog(this);


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

    }

    private void LoginUser()
    {
        String phone =InputPhoneNumber.getText().toString();
        String password =InputPassword.getText().toString();

        if (TextUtils.isEmpty(phone))
        {
            Toast.makeText(this,"Please Write Your Phone Number...", Toast.LENGTH_SHORT).show();
        }
        else if (TextUtils.isEmpty(password))
        {
            Toast.makeText(this,"Please Write Your Password...", Toast.LENGTH_SHORT).show();
        }
        else
        {
            loadingBar.setTitle("Login Account");
            loadingBar.setMessage("Please Wait, while we are checking the credentials.");
            loadingBar.setCanceledOnTouchOutside(false);
            loadingBar.show();

            AllowAccessToAccount(phone, password);
        }
    }

    private void AllowAccessToAccount(final String phone, final String password)
    {
        final DatabaseReference RootRef;
        RootRef = FirebaseDatabase.getInstance().getReference();

        RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if (dataSnapshot.child(parentDbname).child(phone).exists())
                {
                    Users usersData = dataSnapshot.child(parentDbname).child(phone).getValue(Users.class);

                    assert usersData != null;
                    if (usersData.getPhone().equals(phone))
                    {
                        if (usersData.getPassword().equals(password))
                        {
                            Toast.makeText(loginActivity.this, "logged in successfully...", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();

                            Intent intent = new Intent(loginActivity.this, HomeActivity.class);
                            Prevalent.currentOnlineUser = usersData;
                            startActivity(intent);
                        }

                        else
                        {
                            loadingBar.dismiss();
                            Toast.makeText(loginActivity.this, "Password is incorrect", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
                else
                    {
                        Toast.makeText(loginActivity.this, "Account with this" + phone + "number do not exists", Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                        Toast.makeText(loginActivity.this, "you need to create a new account", Toast.LENGTH_SHORT).show();
                    }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {

            }
        });
    }
}

and this is my loginactivity layout

<RelativeLayout 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"
    android:background="@color/orange"
    tools:context=".loginActivity">

    <ImageView
        android:id="@+id/login_applogo"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:src="@drawable/applogo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="170dp"
        />

    <EditText
        android:id="@+id/login_phone_number_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_applogo"
        android:background="@drawable/input_design"
        android:padding="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:hint="Phone Number"
        android:inputType="number"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        android:textSize="17sp"
        android:textStyle="bold"
        />

    <EditText
        android:id="@+id/login_password_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_phone_number_input"
        android:background="@drawable/input_design"
        android:padding="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="6dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        android:textSize="17sp"
        android:textStyle="bold"
        />

    <LinearLayout
        android:id="@+id/linear_layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/login_password_input"
        android:layout_marginTop="5dp"
        >

        <CheckBox
            android:id="@+id/remember_me_checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/Material.Drawable.CheckBox"
            android:text="Remember Me"
            android:textColor="@color/black"
            app:cbd_strokeColor="@color/black"
            android:gravity="center_vertical"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="17dp"
            />

        <TextView
            android:id="@+id/forget_password_link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forgot Password?"
            android:textColor="@color/black"
            android:textSize="17sp"
            android:textStyle="bold"
            android:layout_marginLeft="80dp"
            />

    </LinearLayout>


    <Button
        android:id="@+id/page_login_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linear_layout_1"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/loginbutton"
        android:padding="17dp"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:text="Login"
        android:textColor="@color/black"
        />

    <TextView
        android:id="@+id/admin_panel_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm an Admin"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/page_login_btn"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginLeft="80dp"
        android:layout_marginEnd="23dp"
        />

    <TextView
        android:id="@+id/not_admin_panel_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm an not Admin"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/page_login_btn"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginLeft="80dp"
        android:layout_marginStart="25dp"
        android:visibility="invisible"
        />

</RelativeLayout>

then I get this error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.infinite.fastfood, PID: 12305 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infinite.fastfood/com.infinite.fastfood.loginActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to com.rey.material.widget.Button at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to com.rey.material.widget.Button at com.infinite.fastfood.loginActivity.onCreate(loginActivity.java:36) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)


回答1:


Try changing com.rey.material.widget.Button import to androidx.appcompat.widget.AppCompatButton




回答2:


Try using "Users" instead of parentDbName. if (dataSnapshot.child("Users").child(phone).exists())




回答3:


In firebase, you are creating account for admin, change in firebase from phone: 9999999999 to phone: "9999999999". means add double quote(") in name,phone and password.

if you are not puting double quote in firebase the data is in long datatype. But the java file want all data in string value.



来源:https://stackoverflow.com/questions/57264088/how-to-fix-androidx-appcompat-widget-appcompatbutton-cannot-be-cast-to-com-rey-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!