当我在主要活动中使用开始按钮访问登录活动时,为什么我的应用程序崩溃?

时间:2019-06-23 18:43:46

标签: android

当我在主要活动上单击“开始”按钮时,它在打开登录活动之前崩溃。我该怎么做才能阻止应用崩溃?

我已将应用程序连接到Firebase并编写了我认为正确的代码。请在可能会出错的地方帮助解决代码。

我希望当我在主要活动中单击按钮以打开需要用户电子邮件和密码的登录活动并成功弹出登录时。相反,我收到“已停止”消息。

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>

login.java的代码

package com.kiki.doctorlocation;

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

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

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class login extends AppCompatActivity {

    private EditText txtemail, txtpassword;
    private TextView txtsign_up;
    private Button btnlogin;

    private ProgressDialog progressDialog;

    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();

        progressDialog = new ProgressDialog(this);

        txtemail = (EditText) findViewById(R.id.txtemail);
        txtpassword = (EditText) findViewById(R.id.txtpassword);
        txtsign_up = (TextView) findViewById(R.id.txtsign_up);
        btnlogin = (Button) findViewById(R.id.btnlogin);

        btnlogin.setOnClickListener((View.OnClickListener) this);
        txtsign_up.setOnClickListener((View.OnClickListener) this);
    }

    public void registerUser() {
        String email = txtemail.getText().toString().trim();
        String password = txtpassword.getText().toString().trim();

        if(TextUtils.isEmpty(email)){
            //if email is empty
            Toast.makeText(this, "Email is required", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(password)){
            //if password is empty
            Toast.makeText(this,"Password is required", Toast.LENGTH_SHORT).show();
            return;
        }

        //if user is validated
        progressDialog.setMessage("Logging user in...");
        progressDialog.show();

        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful())
                        {
                            Toast.makeText(login.this, "Log in Successful", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(login.this, "Log in Failed, Please try again", Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }

    public void onClick(View view){
        if(view == btnlogin ){
            registerUser();
        }
        if(view == txtsign_up){
            //open signup activity
        }
    }
}

login.xml的代码

<?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>

2 个答案:

答案 0 :(得分:0)

您的应用程序崩溃是因为您的登录活动未实现OnClickListener,因此您的活动只能实施View.OnClickListener,因此无法投放到其中,

public class login extends AppCompatActivity implements View.OnClickListener { }

答案 1 :(得分:0)

在您的login活动类代码中。请在两行下面进行修改,

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
        }
     }
});

注意:始终使用驼峰命名惯例(例如LoginActivity,而不仅仅是login)命名活动。