Firebase身份验证java.lang.IllegalArgumentException:给定String为空或null

时间:2017-05-06 19:09:00

标签: android firebase firebase-authentication

我尝试了所有其他解决方案。获得了一个新的google-services.json,检查了依赖关系以确定它们都已更新。尝试了所有可能的方法来声明和初始化变量,即使它们没有意义。还是不知道出了什么问题。请看一下,并提前感谢你!

public class SignUp_Fragment extends Fragment implements OnClickListener, AdapterView.OnItemSelectedListener {
private View view;

private TextView login;
private Button signUpButton;
private CheckBox terms_conditions;

EditText editTextFullName, editTextSignUpEmail, editTextMobile,
        editTextSignUpPassword, editTextConfirmPassword;

String signUpEmail;
String signUpPassword;
String mobile;
String fullName;

Spinner locationSpinner;

String stringLocation;

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private User user;


private static FragmentManager fragmentManager;

public SignUp_Fragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.signup_layout, container, false);
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    editTextFullName = (EditText) view.findViewById(R.id.fullName);
    editTextSignUpEmail = (EditText) view.findViewById(R.id.userEmailId);
    editTextMobile = (EditText) view.findViewById(R.id.mobileNumber);
    editTextSignUpPassword = (EditText) view.findViewById(R.id.password);

    signUpEmail = editTextSignUpEmail.getText().toString();
    signUpPassword = editTextSignUpPassword.getText().toString();
    mobile = editTextMobile.getText().toString();

    mAuth = FirebaseAuth.getInstance();

    initViews();
    setListeners();


    return view;
}

// Initialize all views
private void initViews() {

    signUpButton = (Button) view.findViewById(R.id.signUpBtn);
    login = (TextView) view.findViewById(R.id.already_user);
    terms_conditions = (CheckBox) view.findViewById(R.id.terms_conditions);
    locationSpinner = (Spinner) view.findViewById(R.id.location);
    editTextConfirmPassword = (EditText) view.findViewById(R.id.confirmPassword);


    fragmentManager = getActivity().getSupportFragmentManager();


}

private void setListeners() {
    signUpButton.setOnClickListener(this);
    login.setOnClickListener(this);
}

@Override
public void onClick(View v) {



    switch (v.getId()) {
        case R.id.signUpBtn:

            // Call checkValidation method
            if (!checkValidation()) {

                new CustomToast().Show_Toast(getActivity(), view,
                        "تحقق من عدم وجود أخطاء");

            } else {

                onSignUpClicked(view);

            }

            break;

        case R.id.already_user:


            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

            BuySell_Fragment frag = new BuySell_Fragment();

            ft.replace(R.id.frameContainer, frag);
            ft.addToBackStack(null);
            ft.commit();

            break;
    }

}

private boolean checkValidation() {

    String signUpEmail = editTextSignUpEmail.getText().toString();
    String signUpPassword = editTextSignUpPassword.getText().toString();
    String mobile = editTextMobile.getText().toString();
    String confirmPassword = editTextConfirmPassword.getText().toString();
    String fullName = editTextFullName.getText().toString();


    Pattern p = Pattern.compile(Utils.regEx);
    Matcher m = p.matcher(signUpEmail);

    String phoneRegex = (Utils.phoneRegEx);

    if (fullName.equals("") || fullName.length() == 0
            || signUpEmail.equals("") || signUpEmail.length() == 0
            || mobile.equals("") || mobile.length() == 0
            || signUpPassword.equals("") || signUpPassword.length() == 0
            || confirmPassword.equals("")
            || confirmPassword.length() == 0) {

        new CustomToast().Show_Toast(getActivity(), view,
                "جميع الخانات مطلوبة");

        return false;
    }

    // Check if email id valid or not
    else if (!m.find()) {

        new CustomToast().Show_Toast(getActivity(), view,
                "الايميل غير صحيح");

        return false;
    } else if (!mobile.matches(phoneRegex)) {

        new CustomToast().Show_Toast(getActivity(), view,
                "رقم الهاتف غير صحيح");

        return false;
    }


    // Check if both password should be equal
    else if (!confirmPassword.equals(signUpPassword)) {
        new CustomToast().Show_Toast(getActivity(), view,
                "كلمة السر غير متطابقة");
    }

    // Make sure user should check Terms and Conditions checkbox
    else if (!terms_conditions.isChecked()) {

        new CustomToast().Show_Toast(getActivity(), view,
                "المرجو الموافقة على الشروط و الاحكام");

        return false;
    }


    return true;
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String location = parent.getItemAtPosition(position).toString();
    stringLocation = location;


}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

protected void setUpUser() {
    user = new User();
    user.setName(fullName);
    user.setPhoneNumber(mobile);
    user.setEmail(signUpEmail);
    user.setPassword(signUpPassword);
}

public void onSignUpClicked(View view) {
    createNewAccount(signUpEmail, signUpPassword);

}



private void createNewAccount(String email, String password) {

    checkValidation();


    setUpUser();

    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (!task.isSuccessful()) {
                new CustomToast().Show_Toast(getActivity(), view,
                        "محاولة تسجبل فاشلة");
            } else {
                onAuthenticationSucess(task.getResult().getUser());
                new CustomToast().Show_Toast(getActivity(), view,
                        "تم تسجيل حساب بنجاح!");
            }


        }
    });

}

private void onAuthenticationSucess(FirebaseUser mUser) {
    saveNewUser(mUser.getUid(), user.getName(), user.getPhoneNumber(), user.getEmail(), user.getPassword());
    signOut();

    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    Login_Fragment frag = new Login_Fragment();

    ft.replace(R.id.frameContainer, frag);
    ft.addToBackStack(null);
    ft.commit();
}

private void saveNewUser(String userId, String name, String phone, String email, String password) {
    User user = new User(userId, name, phone, email, password);


}


private void signOut() {
    mAuth.signOut();
}




@Override
public void onDetach() {
    super.onDetach();

    try {
        Field fragmentManager = Fragment.class.getDeclaredField("mFragmentManager");
        fragmentManager.setAccessible(true);
        fragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}


public void onDestroy() {
    super.onDestroy();

}

}

app build gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "edu.seu.seniorproject.producingfamily"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-vector-drawable:25.3.1'
    compile 'com.google.android.gms:play-services:10.2.4'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'org.jetbrains:annotations-java5:15.0'
    compile 'com.google.firebase:firebase-storage:10.0.1'
    compile 'com.google.firebase:firebase-auth:10.0.1'
    compile 'com.google.firebase:firebase-core:10.0.1'
    testCompile 'junit:junit:4.12'

}



apply plugin: 'com.google.gms.google-services'

logcat的:

java.lang.IllegalArgumentException: Given String is empty or null

at com.google.android.gms.common.internal.zzac.zzdr(Unknown Source)

at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown Source)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.createNewAccount(SignUp_Fragment.java:250)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.onSignUpClicked(SignUp_Fragment.java:237)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.onClick(SignUp_Fragment.java:128)

at android.view.View.performClick(View.java:5198)

at android.view.View$PerformClick.run(View.java:21147)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

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)

E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.seu.seniorproject.producingfamily, PID: 31055
java.lang.IllegalArgumentException: Given String is empty or null

at com.google.android.gms.common.internal.zzac.zzdr(Unknown Source)

at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown Source)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.createNewAccount(SignUp_Fragment.java:250)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.onSignUpClicked(SignUp_Fragment.java:237)

at edu.seu.seniorproject.producingfamily.fragments.SignUp_Fragment.onClick(SignUp_Fragment.java:128)

at android.view.View.performClick(View.java:5198)

at android.view.View$PerformClick.run(View.java:21147)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

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)

1 个答案:

答案 0 :(得分:1)

您未正确管理signUpEmailsignUpPassword。它们在类级别声明并在onCreateView()中初始化:

    signUpEmail = editTextSignUpEmail.getText().toString();
    signUpPassword = editTextSignUpPassword.getText().toString();

此时,用户尚未在EditTexts中输入任何字符且字符串为空。

稍后,在checkValidation()中,您再次获得EditTexts的内容,但错误地将结果存储在局部变量中而不是类级变量中:

private boolean checkValidation() {
    String signUpEmail = editTextSignUpEmail.getText().toString();
    String signUpPassword = editTextSignUpPassword.getText().toString();

当您尝试创建帐户时,您正在使用仍为空的类级变量。

更改checkValidation()以使用类级变量而不是局部变量:

private boolean checkValidation() {
    signUpEmail = editTextSignUpEmail.getText().toString();
    signUpPassword = editTextSignUpPassword.getText().toString();