繁体   English   中英

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

[英]App keeps crashing every time i click the Log in Button

我是 android 新手,我正在学习一些教程,此时,当我单击登录按钮时,应用程序不断崩溃。 当我运行应用程序并单击登录按钮时,应用程序崩溃并且在 logcat 中,它显示了 java RuntimeException 的这个错误。 这是我的 logcat、loginactivity.xml 和 loginactivity.java。

java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.ugtunda/com.example.ugtunda.LoginActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 无法转换为 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) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:164) 在 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) 引起:java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView 不能转换为 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) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 在 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包)。 尝试进入您的导入并删除com.rey.material行。 然后,当您的 IDE 提示您导入 TextView 和 Checkbox 时,请确保您从android.widget导入版本,而不是com.rey.material 或者,如果您想使用这些组件而不是默认组件,您可以更改您的 XML 文件以指定com.rey.material组件。

你需要更换

import com.rey.material.widget.TextView;

import android.widget.TextView;

.

或者

.

代替

<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"
            />

<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" />

在你的 xml 文件中

androidx.appcompat.widget.AppCompatTextView 不能转换为 com.rey.material.widget.TextView

在您的 xml 中,您使用的是 SDK TextView,但在您的代码中,您需要com.rey.material.widget之一。

--> 更改其中之一

看起来您在 XML 中使用 AppCompat 按钮,但在 java 代码中使用材料按钮。

所以它们必须相同。

首先,您需要为正在使用的任何视图使用正确的导入,如果您想使用 AppCompat 按钮,则像这样导入

import android.widget.Button;

如果要使用“材质”按钮,请使用此导入:

import com.rey.material.widget.Button;

TextView 也是如此。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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