Java 点击注册按钮时应用程序崩溃

Java 点击注册按钮时应用程序崩溃,java,android,Java,Android,我正在使用firebase为应用程序创建一个简单的登录/注册。到目前为止,我只编写了注册功能,每当我在运行它时点击它,应用程序就会崩溃。相关脚本附在下面 注册页面的Java代码: package com.healthandhelpapp; import android.content.Intent; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import

我正在使用firebase为应用程序创建一个简单的登录/注册。到目前为止,我只编写了注册功能,每当我在运行它时点击它,应用程序就会崩溃。相关脚本附在下面

注册页面的Java代码:

package com.healthandhelpapp;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
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 SignUpPage extends AppCompatActivity implements View.OnClickListener {


    private FirebaseAuth mAuth;
    EditText editTextUsername, editTextPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_page);

        editTextUsername=findViewById(R.id.enterEmail);
        editTextPassword=findViewById(R.id.enterPassword);

        mAuth = FirebaseAuth.getInstance();

        findViewById(R.id.butSGUP).setOnClickListener(this);
    }

    private  void registerUser(){

        String email = editTextUsername.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
if(email.isEmpty()){

    editTextUsername.setError("Email is required");

editTextUsername.requestFocus();
    return;
}

          if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){

editTextUsername.setError("Email is invalid");
editTextUsername.requestFocus();

          }


if (password.isEmpty()){


    editTextPassword.setError("Password is required");
    editTextPassword.requestFocus();
    return;
}


if (password.length()<7){

    editTextPassword.setError("Password is too short");
    editTextPassword.requestFocus();
}

mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if(task.isSuccessful()){
            Toast.makeText(getApplicationContext(), "User Registered Successfully", Toast.LENGTH_SHORT).show();


        }
    }
});

    }


    @Override
    public  void onClick(View view){

        switch (view.getId()){

            case R.id.butSGUP:

                registerUser();

                    break;


            case R.id.tgtLogIn:

                startActivity(new Intent(this, LogInPage.class));


        }


    }



}
这是android清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.healthandhelpapp">

<uses-permission android:name="android.permission.INTERNET" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".Intro">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SignUpPage" />
            <activity android:name=".LogInPage"></activity>
        </application>

    </manifest>

编辑2:我已添加权限并更改了编辑文本问题。现在它没有崩溃,但功能仍然没有运行,即用户没有注册

解决方案:

在清单中添加
INTERNET
权限


希望这是您的错误。

您缺少对清单文件的权限。 把这个放在

public class SignUpPage extends AppCompatActivity implements 
View.OnClickListener {


private FirebaseAuth mAuth;
如果不是这样,则在调用的每个函数上初始化它们。我不知道这是否是一个错误或什么,但我已经经历了多次相同的事情。

1)您需要在清单中添加Internet权限

(二)

替换为:-

editTextUsername=findViewById(R.id.enterEmail);
            editTextPassword=findViewById(R.id.editText2);
注意:-由于在xml中找不到视图,因此出现Nullpointer异常

我希望这能解决你的问题

您应该将
if(email.isEmpty())
更改为
if(email.getText().toString().equals(“”)
。我认为现在它总是返回false,因此进入
else
code。因此,在尝试获取
EditText
的字符串时,您会得到
NullPointerException


错误是否总是发生,还是只有当单击事件发生时,
EditText
为空时才会发生?

您可以发布错误吗?请在您询问与崩溃相关的问题时发布错误日志,因为大多数时候答案都在那里。@kdblue我编辑了错误日志,添加了权限,但我似乎没有帮助我看到。。。您应该将edittext声明为private,或者在调用它的每一步初始化它。这应该可以解决问题。我正在编辑我的答案,你的代码已更正!似乎就是这样。它确实不起作用;我不再崩溃,但是函数没有运行,即用户没有注册。编辑:我尝试运行调试器,但遇到以下“运行“应用程序”时出错”:无法打开调试器端口(localhost:8600):java.net.ConnectException“连接被拒绝:连接”“在menifest中拥有internet权限?我有。还为我所做的所有更改编辑了代码
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.healthandhelpapp, PID: 14754
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.healthandhelpapp.SignUpPage.registerUser(SignUpPage.java:38)
                  at com.healthandhelpapp.SignUpPage.onClick(SignUpPage.java:91)
                  at android.view.View.performClick(View.java:6367)
                  at android.view.View$PerformClick.run(View.java:25032)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6753)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
<uses-permission android:name="android.permission.INTERNET" />
public class SignUpPage extends AppCompatActivity implements 
View.OnClickListener {


private FirebaseAuth mAuth;
private EditText editTextUsername, editTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up_page);

    editTextUsername=findViewById(R.id.enterEmail);
    editTextPassword=findViewById(R.id.editText2);
    mAuth = FirebaseAuth.getInstance();

    findViewById(R.id.butSGUP).setOnClickListener(this);
}

private  void registerUser(){
editTextUsername=findViewById(R.id.enterEmail);
        editTextPassword=findViewById(R.id.enteredPassword);
editTextUsername=findViewById(R.id.enterEmail);
            editTextPassword=findViewById(R.id.editText2);