简体   繁体   中英

App keeps crashing every time i click the Log in Button

I am new to android, i was following some tutorial and at this point, app keeps crashing when i click the login button. When i run the app and click the login button, the app crashes and in the logcat, it shows this error of java RuntimeException. This is my logcat, loginactivity.xml and loginactivity.java.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ugtunda/com.example.ugtunda.LoginActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to com.rey.material.widget.TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2884) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1614) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6524) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888) Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to com.rey .material.widget.TextView at com.example.ugtunda.LoginActivity.onCreate(LoginActivity.java:48) at android.app.Activity.performCreate(Activity.java:7044) at android.app.Activity.performCreate(Activity.java:7035) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2759)

Here is my login_activity_XML

<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/login"
    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="200dp"
        />


    <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/colorPrimaryDark"
        android:textColorHint="@color/colorPrimaryDark"
        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/colorPrimaryDark"
        android:textColorHint="@color/colorPrimaryDark"
        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"
        >

        <com.rey.material.widget.CheckBox
            android:id="@+id/remember_me_chkb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/Material.Drawable.CheckBox"
            android:text="Remember Me"
            android:textColor="@color/colorPrimaryDark"
            app:cbd_strokeColor="@color/colorPrimaryDark"
            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="Forget Password"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginLeft="80dp"
            />
    </LinearLayout>


    <Button
        android:id="@+id/login_button"
        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/buttons"
        android:padding="17dp"
        android:text="Login"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="18sp" />


    <TextView
        android:id="@+id/admin_panel_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_button"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="80dp"
        android:layout_marginEnd="23dp"
        android:text="I'm an Admin"
        android:textAppearance="@android:style/TextAppearance.Material.Small"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp"
        android:textStyle="bold" />

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

</RelativeLayout>

And lastly my loginactivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ugtunda.Models.Users;
import com.example.ugtunda.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.widget.CheckBox;
import com.rey.material.widget.TextView;

import io.paperdb.Paper;

public class LoginActivity extends AppCompatActivity
{
    private EditText InputPhoneNumber, InputPassword;
    private Button LoginButton;
    private ProgressDialog loadingBar;
    private TextView AdminLink, NotAdminLink;

    private String parentDbName = "Users";
    private CheckBox chkBoxRememberMe;


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


        LoginButton = findViewById(R.id.login_button);
        InputPassword = findViewById(R.id.login_password_input);
        InputPhoneNumber = findViewById(R.id.login_phone_number_input);
        AdminLink = findViewById(R.id.admin_panel_link);
        NotAdminLink = findViewById(R.id.not_admin_panel_link);
        loadingBar = new ProgressDialog(this);


        chkBoxRememberMe = findViewById(R.id.remember_me_chkb);
        Paper.init(this);


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

        AdminLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                LoginButton.setText("Login Admin");
                AdminLink.setVisibility(View.INVISIBLE);
                NotAdminLink.setVisibility(View.VISIBLE);
                parentDbName = "Admins";
            }
        });

        NotAdminLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                LoginButton.setText("Login");
                AdminLink.setVisibility(View.VISIBLE);
                NotAdminLink.setVisibility(View.INVISIBLE);
                parentDbName = "Users";
            }
        });
    }



    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)
    {
        if(chkBoxRememberMe.isChecked())
        {
            Paper.book().write(Prevalent.USER_PHONE_KEY, phone);
            Paper.book().write(Prevalent.USER_PASSWORD_KEY, 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);

                    if (usersData.getPhone().equals(phone))
                    {
                        if (usersData.getPassword().equals(password))
                        {
                            if (parentDbName.equals("Admins"))
                            {
                                Toast.makeText(LoginActivity.this, "Welcome Admin, you are logged in Successfully...", Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();

                                Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
                                startActivity(intent);
                            }
                            else if (parentDbName.equals("Users"))
                            {
                                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();
                }
            }

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

            }
        });
    }
}

It looks like you're defining standard Android components in the XML layout, then attempting to inflate them into custom components (the com.rey.material package). Try going into your imports and deleting the com.rey.material lines. Then when your IDE prompts you to import the TextView and Checkbox, make sure you're importing the versions from android.widget , not com.rey.material . Alternatively, you could change your XML file to specify the com.rey.material components if you wanted to be using those instead of the default ones.

You need to replace

import com.rey.material.widget.TextView;

with

import android.widget.TextView;

.

OR

.

Replace

<TextView
            android:id="@+id/forget_password_link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forget Password"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginLeft="80dp"
            />

with

<com.rey.material.widget.TextView
            android:id="@+id/forget_password_link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forget Password"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginLeft="80dp" />

in your xml file

androidx.appcompat.widget.AppCompatTextView cannot be cast to com.rey.material.widget.TextView

In your xml, you're using the SDK TextView but in your code, you want the com.rey.material.widget one.

--> Change one of these

Looks like you are using the AppCompat Button in your XML, but using the material button in your java code.

So they need to be the same.

First you need to use the right import for whatever view you're using, if you want to use the AppCompat button then import like this

import android.widget.Button;

If you want to use the Material button, then use this import:

import com.rey.material.widget.Button;

Same thing goes for the TextView.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM