java - 每次我单击“登录”按钮时,应用程序都会崩溃

标签 java android xml

我是 Android 新手,我正在遵循一些教程,此时,当我单击登录按钮时,应用程序不断崩溃。 当我运行应用程序并单击登录按钮时,应用程序崩溃并在 logcat 中显示 java RuntimeException 错误。 这是我的logcat、loginactivity.xml 和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)

这是我的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>

最后是我的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) {

            }
        });
    }
}

最佳答案

看起来您正在 XML 布局中定义标准 Android 组件,然后尝试将它们扩展为自定义组件(com.rey.material 包)。尝试进入导入并删除 com.rey.material 行。然后,当您的 IDE 提示您导入 TextView 和 Checkbox 时,请确保您从 android.widget 导入版本,而不是 com.rey.material。或者,如果您想使用这些组件而不是默认组件,则可以更改 XML 文件以指定 com.rey.material 组件。

关于java - 每次我单击“登录”按钮时,应用程序都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995112/

相关文章:

java - 多个 SHELL channel JSch java SSH

java - Restful jboss 反序列化

android - 没有ui的后台应用

java - 如何将 web 服务的 xml 响应转换为所需的 xml 格式?

c++ - 从 UnicodeString 创建 CData 节点时的 XML 无效字符

Java 的 "Scanner"方法 vs. Facade GoF 设计模式

Java 在 SwingWorker 中渲染游戏 map 无法正常工作

Android + HTML5(本地存储) + Admob : Bug?

java - 在 TextView 中显示数组元素

c# - XmlSerializer 反序列化具有不同元素名称的列表