android.text.editable android.widget.edittext.gettext()在空对象引用上

dgjrabp2  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(683)

我创建了一个应用程序,在其中创建了登录和注册活动,并对其java文件进行了编码。当我执行它,并点击注册后填写所有细节,应用程序突然关闭。然后我在运行面板中搜索了这个问题及其下面的错误。我尽我所能,但找不到主要原因。请帮我解决这个问题。我受够了。下面的错误显示在register.java中

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.marvi, PID: 28256
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.marvi.register$1.onClick(register.java:67)
        at android.view.View.performClick(View.java:6256)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

有人能告诉我为什么会这样吗
登录.java

package com.example.marvi;

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

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.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 {
    EditText memail , mpass ;
    FirebaseAuth fAuth;

    Button mlogin;
    TextView mcreate , mforgot;

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

        mlogin = findViewById(R.id.signbtn);
        mcreate = findViewById(R.id.create);
        mforgot = findViewById(R.id.forgot);
        fAuth = FirebaseAuth.getInstance();
        memail = findViewById(R.id.email);
        mpass = findViewById(R.id.passbox);

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = memail.getText().toString().trim();
                String password = mpass.getText().toString().trim();

                if(TextUtils.isEmpty((email))){
                    memail.setError("E-mail is required");
                }

                if(TextUtils.isEmpty((password))){
                    memail.setError("Password is required");
                }

                if(password.length() <6){
                    mpass.setError("Password must be > 6");
                }

                //authenticate
                fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(login.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else{
                            Toast.makeText(login.this, "Error !"+ task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });

        mcreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),register.class));
                finish();
            }
        });

    }
}

寄存器.java

package com.example.marvi;

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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
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 com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class register extends AppCompatActivity {
    public static final String TAG = "TAG";
    EditText mfullname,memail,mpass,mphone;
    FirebaseAuth fAuth;
    RadioButton mcheckmale,mcheckfemale;
    String userID;
    String gender="";
    FirebaseFirestore fStore;

    Button mlogin;
    TextView mcreate , mforgot;

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

        mfullname = findViewById(R.id.name);
        memail = findViewById(R.id.email);
        mpass   = findViewById(R.id.pass);
        mphone      = findViewById(R.id.number);
        mcheckmale= findViewById(R.id.malebtn);
        mcheckfemale  = findViewById(R.id.femalebtn);
        fStore = FirebaseFirestore.getInstance();
        fAuth = FirebaseAuth.getInstance();

        mlogin = findViewById(R.id.signbtn);
        mcreate = findViewById(R.id.create);
        mforgot = findViewById(R.id.forgot);

        if(fAuth.getCurrentUser() !=null){
            Toast.makeText(register.this, "user created", Toast.LENGTH_SHORT).show();
            finish();
        }

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = memail.getText().toString().trim();
                String password = mpass.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    memail.setError("Email is Required.");
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    mpass.setError("Password is Required.");
                    return;
                }

                if(password.length() < 6){
                    mpass.setError("Password Must be >= 6 Characters");
                    return;
                }

                if(mcheckmale.isChecked()){
                    gender="Male";
                }
                if(mcheckfemale.isChecked()){
                    gender="Female";
                }

                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(register.this, "user created", Toast.LENGTH_SHORT).show();
                            userID = fAuth.getCurrentUser().getUid();
                            DocumentReference documentReference = fStore.collection("users").document(userID);
                            Map<String,Object> user = new HashMap<>();
                            user.put("email",email);
                            user.put("pass",password);
                            user.put("gender",gender);

                            documentReference.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    Log.d(TAG, "onSuccess: user profile is created for " +userID);

                                }
                            });

                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else{
                            Toast.makeText(register.this, "Error !"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        mcreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),login.class));
                finish();
            }
        });

    }
}
bjp0bcyl

bjp0bcyl1#

在oncreate()方法上示例化如下编辑文本:

memail = findViewById(R.id.yourEmailFieldID);
mpass = findViewById(R.id.yourPasswordFieldID);

相关问题