我一直得到" java.lang.IllegalArgumentException:给定String为空或null"它与auth = FirebaseAuth.getInstance();"有关。

时间:2016-10-22 18:27:11

标签: java android firebase firebase-authentication

我正在为firebase中的用户注册创建一个简单的应用程序。我添加了两个editText用于从用户获取电子邮件和密码以及用于注册的按钮。我已经添加了一个扩展Application的类来初始化默认的firebaseApp。所有代码都添加在下面。帮我排序这个错误。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

        EditText ed_email, ed_pass;
        Button but_signup;

        ProgressBar progressBar;
        FirebaseAuth auth;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //FirebaseApp.initializeApp(getApplicationContext());
            //Get Firebase auth instance
            auth = FirebaseAuth.getInstance();


            but_signup = (Button) findViewById(R.id.but_login);

            ed_email = (EditText) findViewById(R.id.ed_email);
            ed_pass = (EditText) findViewById(R.id.ed_pass);


            but_signup.setOnClickListener(this);
        }

        @Override
        protected void onResume() {
            super.onResume();
        }

        @Override
        public void onClick(View v) {
            String email = ed_email.getText().toString().trim();
            String password = ed_pass.getText().toString().trim();

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 6) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                return;
            }


            //create user
            auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();

                            if (!task.isSuccessful()) {
                                Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                startActivity(new Intent(MainActivity.this, MainActivity.class));
                                finish();
                            }
                        }
                    });
        }
    }

FireApp.java

public class FireApp extends Application {
@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(getApplicationContext());

}}

logcat的

FATAL EXCEPTION: main
Process: com.skapsdevelopment.firebase, PID: 13582
Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:com.baranovgroup.nstyle, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skapsdevelopment.firebase/com.skapsdevelopment.firebase.MainActivity}: java.lang.IllegalArgumentException: Given String is empty or null
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
Caused by: java.lang.IllegalArgumentException: Given String is empty or null
   at com.google.android.gms.common.internal.zzac.zzhz(Unknown Source)
   at com.google.android.gms.internal.zzagt$zza$zza.<init>(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.zzb(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.<init>(Unknown Source)
   at com.google.android.gms.internal.zzahe.<init>(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.zzd(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.zzc(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
   at com.skapsdevelopment.firebase.MainActivity.onCreate(MainActivity.java:36)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 

请帮我解决这个错误。

2 个答案:

答案 0 :(得分:1)

如果未定义auth键字符串(通常称为Api键),则会发生此错误。因此,请检查google-services.json文件并将您的密钥放在current_key标记中。然后重新构建项目,以便应用程序使用api密钥。

答案 1 :(得分:0)

我的问题是我的google_services.json中的api_key值为空。这是因为firebase为我生成的默认json文件根本没有api_key值,所以我只是手动添加一个以使其工作。在此处添加随机字符串值将使codelab工作,但请确保停止当前运行并重建项目,否则Android Studio自动运行功能将在不更新正在使用的json文件的情况下运行。

你可以从中获取你的api密钥。 /console.firebase.google.com/project - &gt;你的项目 - &gt;设置 - &gt;一般。

并将其添加到google_services.json current_key