android app按钮onClick Listener错误

时间:2017-12-28 23:47:47

标签: java android firebase firebase-authentication

我在下面编写此代码,为用户提供了一个简单的注册选项,当我按下创建我的帐户按钮时,我得到: 任何人都可以弄明白为什么?不能自己搞清楚。 提前感谢大家

  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~   12-28 18:38:57.792 3358-3358 / gr.aegean.icsd15109.myfitup   E / AndroidRuntime:致命异常:主要                                                                              处理:gr.aegean.icsd15109.myfitup,PID:3358                                                                              显示java.lang.NullPointerException                                                                                  在   gr.aegean.icsd15109.myfitup.RegisterActivity.registerUser(RegisterActivity.java:51)                                                                                  在   gr.aegean.icsd15109.myfitup.RegisterActivity $ 1.onClick(RegisterActivity.java:45)                                                                                  在android.view.View.performClick(View.java:4438)                                                                                  在android.view.View $ PerformClick.run(View.java:18422)                                                                                  在android.os.Handler.handleCallback(Handler.java:733)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:136)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)                                                                                  在java.lang.reflect.Method.invoke(Method.java:515)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                                  在dalvik.system.NativeStart.main(本地方法)

     

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~

CODE:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;

import org.w3c.dom.Text;

public class RegisterActivity extends AppCompatActivity {

    private EditText mDisName;
    private EditText mMail;
    private EditText mPass;
    private Button reg;
    private TextView error;
    private FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mDisName =(EditText) findViewById(R.id.reg_name);
        mMail = (EditText) findViewById(R.id.reg_mail);
        mPass = (EditText) findViewById(R.id.reg_password);
        reg = (Button) findViewById(R.id.reg_button);
        error = (TextView) findViewById(R.id.textView4);

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String disName = mDisName.getText().toString();
                String dmail = mMail.getText().toString();
                String pass = mPass.getText().toString();
                registerUser(disName,dmail,pass);
            }
        });
    }

    public void registerUser(String display_name,String email,String password){
        mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                        Intent loginIntent = new Intent(RegisterActivity.this,login.class);
                        startActivity(loginIntent);
                        finish();
                }
                else{
                    Toast.makeText(RegisterActivity.this,"Error with registration!",Toast.LENGTH_LONG).show();
                    error.setText("Error 110! User already exists! \n Please provide different username!");
                }
            }
        });

    }
}

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="gr.aegean.icsd15109.myfitup.account">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/reg_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/display_name" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout3"
        android:layout_width="330dp"
        android:layout_height="62dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/reg_mail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/email" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/reg_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="56dp"
        android:text="@string/create_account"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reg_password"
        tools:ignore="RtlHardcoded" />

    <EditText
        android:id="@+id/reg_password"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:ems="10"
        android:hint="@string/password2"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.526"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="262dp"
        android:layout_height="55dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="44dp"
        android:textColor="@android:color/holo_red_dark"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reg_button" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

代码中的

mAuth为空..你没有得到它的实例

这样做:

在onCreate()方法中,初始化FirebaseAuth实例:

mAuth = FirebaseAuth.getInstance();

然后你就可以使用这个方法了:

 mAuth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

了解更多信息,请查看:https://firebase.google.com/docs/auth/android/start/