当前位置: 首页 > 知识库问答 >
问题:

在Android studio中,如果我试图在没有填写必填字段的情况下单击帐户创建btn,应用程序就会崩溃

楚皓君
2023-03-14

`

    auth = FirebaseAuth.getInstance();
    database = FirebaseFirestore.getInstance();

    dialog = new ProgressDialog(this);
    dialog.setMessage("We're creating new account...");
    String email, pass, name, referCode;

    email = binding.emailBox.getText().toString();
    pass = binding.passwordBox.getText().toString();
    name = binding.nameBox.getText().toString();
    referCode = binding.referBox.getText().toString();
    if((email!= null ) &&
            ( pass!= null) &&
            (name != null)  )
    {
        binding.createNewBtn.setEnabled(true);
    }else{
        binding.createNewBtn.setEnabled(false);
    }
    binding.createNewBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email, pass, name, referCode;

            email = binding.emailBox.getText().toString();
            pass = binding.passwordBox.getText().toString();
            name = binding.nameBox.getText().toString();
            referCode = binding.referBox.getText().toString();

            final User user = new User(name, email, pass, referCode);
            dialog.show();
            auth.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()) {
                        String uid = task.getResult().getUser().getUid();

                        database
                                .collection("users")
                                .document(uid)
                                .set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if(task.isSuccessful()) {
                                    dialog.dismiss();
                                    startActivity(new Intent(SignupActivity.this, MainActivity.class));
                                    finish();
                                } else {
                                    Toast.makeText(SignupActivity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } else {
                        dialog.dismiss();
                        Toast.makeText(SignupActivity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
    });

`**java。lang.IllegalArgumentException:给定字符串在com处为空或null。谷歌。Androidgms。常见的内部的前提条件。在com上选中notempty(com.google.android.gms:play services base@@17.3.0:5)。谷歌。firebase。授权。FirebaseAuth。在com上创建UserWithEmailandPassword(com.google.firebase:firebase auth@@20.0.2:1)。詹姆斯。泰米尔基奇。注册活动1美元。android上的onClick(SignupActivity.java:64)。看法看法android上的performClick(View.java:8160)。小装置。文本视图。android上的performClick(TextView.java:16222)。看法看法在android上执行ClickInternal(View.java:8137)。看法看法在android上访问3700美元(View.java:888)。看法查看$PerformClick。在android上运行(View.java:30236)。操作系统。处理程序。android上的handleCallback(Handler.java:938)。操作系统。处理程序。android上的dispatchMessage(Handler.java:99)。操作系统。活套。android上的loop(Looper.java:246)。应用程序。ActivityThread。java上的main(ActivityThread.java:8633)。lang.reflect。方法在com上调用(本机方法)。Android内部的操作系统。RuntimeInit$MethodandArgscaler。在com上运行(RuntimeInit.java:602)。Android内部的操作系统。合子岩。主(ZygoteInit.java:1130)I/Process:发送信号。PID:21407信号:9**

共有1个答案

欧阳永宁
2023-03-14

在创建用户之前,应检查必填字段是否为非空:

String email, pass, name, referCode;

email = binding.emailBox.getText().toString().trim();
pass = binding.passwordBox.getText().toString().trim();
name = binding.nameBox.getText().toString().trim();
referCode = binding.referBox.getText().toString().trim();

// Add these field controls
if (email == "") {
    Toast.makeText(SignupActivity.this, "E-mail is required", Toast.LENGTH_SHORT).show();
    return 
}
if (pass == "") {
    Toast.makeText(SignupActivity.this, "Password is required", Toast.LENGTH_SHORT).show();
    return 
}

final User user = new User(name, email, pass, referCode);
dialog.show();
auth.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
 类似资料: