java.lang.IllegalArgumentException:执行Firebase时,给定String为空或为null updatePassword()方法

时间:2016-10-04 00:46:51

标签: android firebase firebase-authentication

我目前正在使用Firebase Real-time DB处理我的项目,但是当我执行它给我的方法时,我遇到了一个问题:

 E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: naufal.com.tugasakhir, PID: 7291
                                             java.lang.IllegalArgumentException: Given String is empty or null
                                                 at com.google.android.gms.common.internal.zzaa.zzdl(Unknown Source)
                                                 at com.google.firebase.auth.FirebaseUser.updatePassword(Unknown Source)
                                                 at naufal.com.tugasakhir.ChangePass$1.onClick(ChangePass.java:59)
                                                 at android.view.View.performClick(View.java:5246)
                                                 at android.widget.TextView.performClick(TextView.java:10620)
                                                 at android.view.View$PerformClick.run(View.java:21256)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:145)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

这是我的updatePassword()方法

mUser.updatePassword(repass).addOnCompleteListener(ChangePass.this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(ChangePass.this, "Update Password Failed",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        startActivity(new Intent(ChangePass.this, MainActivity.class));
                        finish();
                    }

                }
            });

这是我的getCurrentUser()对象初始化

        final FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();

该对象超出了onCreate()方法,因此必须使用final。

    package naufal.com.tugasakhir;

import android.content.Intent;
import android.nfc.Tag;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firebase.client.Firebase;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class ChangePass extends AppCompatActivity {

    private EditText mChangepass,mReChangePass;
    private Button mChangeBtn;
    private static final String TAG = ChangePass.class.getSimpleName();

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

        mChangepass = (EditText) findViewById(R.id.changePass);
        mReChangePass = (EditText) findViewById(R.id.reChangePass);
        mChangeBtn = (Button) findViewById(R.id.changeBtn);

        final FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();

        mChangeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String pass = mChangepass.getText().toString().trim();
                String repass = mReChangePass.getText().toString().trim();

                if(pass.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Enter New Password", Toast.LENGTH_SHORT).show();
                }
                if(repass.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Enter New Password", Toast.LENGTH_SHORT).show();
                }

                if(pass.length()<6){
                    Toast.makeText(getApplicationContext(), "Password is too Short (Minimum 6)", Toast.LENGTH_SHORT).show();
                }
                if(!pass.equals(repass)){
                    Toast.makeText(getApplicationContext(), "Password Does Not Match", Toast.LENGTH_SHORT).show();
                }

                assert mUser != null;
                mUser.updatePassword(repass).addOnCompleteListener(ChangePass.this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(ChangePass.this, "Update Password Failed",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            startActivity(new Intent(ChangePass.this, MainActivity.class));
                            finish();
                        }

                    }
                });

        });


    }
}

谢谢你的回复。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。因为:google-services.json中的字段"current_key":""

*由 解决,获取新的google-services.json 。并确保它具有有效性&#34; current_key&#34;。

"api_key": [
        {
          "current_key": "validity-key-xxxxxxxxxxxxxxx"
        }
   ]

enter image description here

答案 1 :(得分:0)

来自OP的帖子:我忘记在功能中添加返回。