java.lang.String com.google.firebase.auth.FirebaseUser.getUid()方法出错

时间:2018-08-27 08:13:27

标签: android firebase nullpointerexception firebase-authentication

请帮助解决以下问题: 我的代码很简单:

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;

public class SetupActivity extends AppCompatActivity {

    private EditText UserName,FullName,CountryName;
    private Button SaveInformationButton;
    private CircleImageView ProfileImage;
    private ProgressDialog loadingBar;

    private FirebaseAuth mAuth;
    private DatabaseReference UsersRef;

    String currentUserID;



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

        mAuth=FirebaseAuth.getInstance();
        currentUserID =mAuth.getCurrentUser().getUid();
        UsersRef= FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);

        UserName=(EditText) findViewById(R.id.setup_username);
        FullName=(EditText) findViewById(R.id.setup_full_name);
        CountryName=(EditText) findViewById(R.id.setup_country_name);

        SaveInformationButton=(Button) findViewById(R.id.setup_information_button);
        ProfileImage=(CircleImageView) findViewById(R.id.setup_profile_image);
        loadingBar=new ProgressDialog(this);


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

    }

    private void SaveAccountSetupInformation()
    {
        String username=UserName.getText().toString();
        String fullname=FullName.getText().toString();
        String country=CountryName.getText().toString();

        if(TextUtils.isEmpty(username)){
            Toast.makeText(this, "Please write your username", Toast.LENGTH_SHORT).show();
        }
        if(TextUtils.isEmpty(fullname)){
            Toast.makeText(this, "Please write your full name", Toast.LENGTH_SHORT).show();
        }
        if(TextUtils.isEmpty(country)){
            Toast.makeText(this, "Please write your country", Toast.LENGTH_SHORT).show();
        }
        else {

            HashMap userMap=new HashMap();
            userMap.put("username",username);
            userMap.put("fullname",fullname);
            userMap.put("country",country);

            UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener()
            {
                @Override
                public void onComplete(@NonNull Task task)
                {
                    if(task.isSuccessful())
                    {
                        SendUserToMainActivity();
                        Toast.makeText(SetupActivity.this, "Your account is created successfully!", Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                    else
                    {
                        String message=task.getException().getMessage();
                        Toast.makeText(SetupActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                }
            });


        }
    }

    private void SendUserToMainActivity() {
        Intent mainIntent=new Intent(SetupActivity.this,MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }
}

但是当我运行它时,我得到一个错误: 致命异常:主要                   流程:com.example.android.socialnetwork,PID:13974                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.socialnetwork / com.example.android.socialnetwork.SetupActivity}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String com。 google.firebase.auth.FirebaseUser.getUid()”上的空对象引用                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)

0 个答案:

没有答案