简体   繁体   中英

Given String is empty or null

quickie!! I'm not much experienced with debugging but I'm struggling to fix this. It might be obvious for you guys but if someone can help me it would be good

This is the error :

2019-03-12 04:12:59.163 17226-17226/com.app.mk.transport E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.mk.transport, PID: 17226
    java.lang.IllegalArgumentException: Given String is empty or null
        at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source:11)
        at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source:0)
        at com.app.mk.transport.MainActivity.showLogInDialog(MainActivity.java:119)
        at com.app.mk.transport.MainActivity.access$100(MainActivity.java:29)
        at com.app.mk.transport.MainActivity$2.onClick(MainActivity.java:65)
        at android.view.View.performClick(View.java:6897)
        at android.widget.TextView.performClick(TextView.java:12693)
        at android.view.View$PerformClick.run(View.java:26101)
        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:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
2019-03-12 04:12:59.214 17226-17233/com.app.mk.transport I/zygote64: Do partial code cache collection, code=55KB, data=47KB
2019-03-12 04:12:59.215 17226-17233/com.app.mk.transport I/zygote64: After code cache collection, code=55KB, data=47KB
2019-03-12 04:12:59.215 17226-17233/com.app.mk.transport I/zygote64: Increasing code cache capacity to 256KB
2019-03-12 04:12:59.216 17226-17233/com.app.mk.transport I/zygote64: Compiler allocated 9MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)

This is my MainActivity class:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Firebase Init
        auth = FirebaseAuth.getInstance();
        db = FirebaseDatabase.getInstance();
        users = db.getReference("Users");

        // Init View
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnSignIn = (Button) findViewById(R.id.btnSignIn);
        rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);

        //Event
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRegisterDialog();
            }
        });

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogInDialog();
            }
        });

    }

    private void showLogInDialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("SIGN IN ");
        dialog.setMessage("Please use email to sign in!");

        LayoutInflater inflater = LayoutInflater.from(this);

        View layout_login = inflater.inflate(R.layout.layout_login, null);

        final MaterialEditText edtEmail = layout_login.findViewById(R.id.edtEmail);
        final MaterialEditText edtPassword = layout_login.findViewById(R.id.edtPassword);

        dialog.setView(layout_login);

        //set button
        dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();

                btnSignIn.setEnabled(false);

                //Check validation

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter your Password!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password is too short!", Snackbar.LENGTH_SHORT).show();
                    return;
                }
            }

        });


        final SpotsDialog waitingDialog = new SpotsDialog(MainActivity.this, "Loading...", R.style.Orange);

        waitingDialog.show();

        //Login
        auth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                    @Override
                    public void onSuccess(AuthResult authResult) {
                        waitingDialog.dismiss();
                        startActivity(new Intent(MainActivity.this, Welcome.class));
                        finish();
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                waitingDialog.dismiss();

                Snackbar.make(rootLayout, "Failed " + e.getMessage(), Snackbar.LENGTH_SHORT).show();

                btnSignIn.setEnabled(true);
            }
        });

        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();

            }
        });


        dialog.show();
    }

The problem is, by clicking my "Log in" button the error pops up. Not sure what's going on. The other button "Register" works just fine, throws data in the firebase but the Sign IN dialog is not even opening.

If you can give me straight answer on what to change or how to fix it, it will be really helpful for me. Greetings Mitkashin-

You need to move your sign in code into your positive button on click.//Login

 dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();

                btnSignIn.setEnabled(false);

                //Check validation

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter your Password!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password is too short!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                auth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                    @Override
                    public void onSuccess(AuthResult authResult) {
                        waitingDialog.dismiss();
                        startActivity(new Intent(MainActivity.this, Welcome.class));
                        finish();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                   @Override
                   public void onFailure(@NonNull Exception e) {
                      waitingDialog.dismiss();

                      Snackbar.make(rootLayout, "Failed " + e.getMessage(), Snackbar.LENGTH_SHORT).show();

                      btnSignIn.setEnabled(true);
                  }
               });


            }

        });

You are putting signin code in the wrong place. You need to put it inside your Signin button click listener like this:

dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss();

        btnSignIn.setEnabled(false);

        //Check validation

        if (TextUtils.isEmpty(edtEmail.getText().toString())) {
            Snackbar.make(rootLayout, "Please enter email address!", Snackbar.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(edtPassword.getText().toString())) {
            Snackbar.make(rootLayout, "Please enter your Password!", Snackbar.LENGTH_SHORT).show();
            return;
        }

        if (edtPassword.getText().toString().length() < 6) {
            Snackbar.make(rootLayout, "Password is too short!", Snackbar.LENGTH_SHORT).show();
            return;
        }

        //Login
        auth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    waitingDialog.dismiss();
                    startActivity(new Intent(MainActivity.this, Welcome.class));
                    finish();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    waitingDialog.dismiss();

                    Snackbar.make(rootLayout, "Failed " + e.getMessage(), Snackbar.LENGTH_SHORT).show();

                    btnSignIn.setEnabled(true);
                }
        });
    }

});

As others have said, your call to auth.signInWithEmailAndPassword() is in the wrong place. You're calling it in the method that builds your AlertDialog , so it's run as soon as you press the button instead of running when the Sign In button is pressed. The dialog doesn't show because your app crashes before it's done building the dialog box.

    java.lang.IllegalArgumentException: Given String is empty or null
    at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source:11)
    at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source:0)

These first few lines from the stack trace explain very clearly that signInWithEmailAndPassword() is being given bad arguments. I can see from your question that you seem to be in a real hurry to fix this, but please take the time to read and understand the traces. They're usually very helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM