Android 当我使用主活动中的开始按钮访问登录活动时,为什么我的应用程序会崩溃?
当我单击主活动上的“开始”按钮时,在它打开登录活动之前崩溃。如何阻止应用程序崩溃 我已经将应用程序连接到firebase,并编写了我认为正确的代码。请帮助解决我可能弄错的代码 我希望当我单击主活动中的按钮时,打开需要用户电子邮件和密码的登录活动,并弹出成功登录的窗口。相反,我收到一条“已停止”的消息Android 当我使用主活动中的开始按钮访问登录活动时,为什么我的应用程序会崩溃?,android,Android,当我单击主活动上的“开始”按钮时,在它打开登录活动之前崩溃。如何阻止应用程序崩溃 我已经将应用程序连接到firebase,并编写了我认为正确的代码。请帮助解决我可能弄错的代码 我希望当我单击主活动中的按钮时,打开需要用户电子邮件和密码的登录活动,并弹出成功登录的窗口。相反,我收到一条“已停止”的消息 MainActivity.java的代码 package com.kiki.doctorlocation; import androidx.annotation.NonNull; import a
MainActivity.java的代码
package com.kiki.doctorlocation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListner;
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListner);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.startbtn);
mAuth = FirebaseAuth.getInstance();
mAuthListner = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
startActivity(new Intent(MainActivity.this, login.class));
}
}
};
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
}
});
}
}
MainActivity.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"
android:background="@drawable/docto"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/startbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="START"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:background="@drawable/roundbtn"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/doc"
android:orientation="vertical"
android:gravity="center"
tools:context=".login">
<EditText
android:id="@+id/txtemail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/roundsedttxt"
android:hint="Enter email"
android:textAlignment="center"
android:inputType="textEmailAddress"
android:layout_margin="10dp"/>
<EditText
android:id="@+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/roundsedttxt"
android:hint="Password"
android:textAlignment="center"
android:inputType="textPassword"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/roundbtn"
android:text="Login"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/txtsign_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create a new account"
android:textStyle="bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textAppearance="@color/common_google_signin_btn_text_dark"
android:textColor="#FFFFFF"
android:layout_margin="10dp"/>
</LinearLayout>
login.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"
android:background="@drawable/docto"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/startbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="START"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:background="@drawable/roundbtn"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/doc"
android:orientation="vertical"
android:gravity="center"
tools:context=".login">
<EditText
android:id="@+id/txtemail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/roundsedttxt"
android:hint="Enter email"
android:textAlignment="center"
android:inputType="textEmailAddress"
android:layout_margin="10dp"/>
<EditText
android:id="@+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/roundsedttxt"
android:hint="Password"
android:textAlignment="center"
android:inputType="textPassword"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/roundbtn"
android:text="Login"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/txtsign_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create a new account"
android:textStyle="bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textAppearance="@color/common_google_signin_btn_text_dark"
android:textColor="#FFFFFF"
android:layout_margin="10dp"/>
</LinearLayout>
您的应用程序崩溃,因为您的登录活动没有实现OnClickListener,因此您的活动不能被强制转换到它中。只需实现View.OnClickListener,您就可以这样做:
public class login extends AppCompatActivity implements View.OnClickListener { }
在您的登录中
活动类代码。请修改以下两行
btnlogin.setOnClickListener((View.OnClickListener) this);
txtsign_up.setOnClickListener((View.OnClickListener) this);
像这样,
btnlogin.setOnClickListener(clickListener);
txtsign_up.setOnClickListener(clickListener);
将此clickListener
添加为全局变量
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view.getId() == R.id.btnlogin){
registerUser();
} else if(view.getId() == R.id.txtsign_up){
//open signup activity
}
}
});
注意:始终使用camelCase命名约定命名您的活动,如LoginActivity
,而不仅仅是登录
请添加错误日志您的登录
活动是否继承(实现)了视图。OnClickListener
界面?似乎没有,因此当您将onCreate
方法设置为onclicklistener时,它可能会导致ClassCastException
,请告诉我如何操作。我正在自学安卓,非常感谢你,代码刚刚好用