FirebaseAuth null对象

时间:2017-07-22 14:03:13

标签: android firebase nullpointerexception firebase-authentication

Auth null我不知道问题。为什么我不能在我的项目中使用Auth Firebase。

public class LoginOrg extends AppCompatActivity {

    private TextView mAddroom , mManageroom , tvName;
    private Button LogoutBtn;
    private String name,PositionUid;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;

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

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            name = bundle.getString("putName");
        }

        PositionUid = mAuth.getCurrentUser().getUid();

        bindWidget();
        IntentOrg();
        onStart();

    }

    private void bindWidget() {
        mManageroom = (TextView) findViewById(R.id.manageRoomorg);
        mAddroom = (TextView) findViewById(R.id.addRoomorg);

        tvName = (TextView) findViewById(R.id.tvName);
        tvName.setText(name);
        LogoutBtn = (Button)findViewById(R.id.btnLogout);

        mAuth = FirebaseAuth.getInstance();
        mAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() == null) {
                    startActivity(new Intent(LoginOrg.this, PageLogin.class));
                    finish();
                }else {

                }
            }
        };

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

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthStateListener);
    }


    private void IntentOrg() {
        mManageroom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginOrg.this, OrgManageRoom.class);
                i.putExtra("putName", tvName.getText().toString());
                i.putExtra("putUid", PositionUid.toString());
                startActivity(i);
            }
        });

        mAddroom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginOrg.this,OrgRoom.class);
                i.putExtra("putName",tvName.getText().toString());
                startActivity(i);
            }
        });

    }
}

报告错误:

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'

1 个答案:

答案 0 :(得分:2)

    PositionUid = mAuth.getCurrentUser().getUid();

    bindWidget();

mAuth = FirebaseAuth.getInstance()来自bindWidget(),您尝试在mAuth之前致电..... {/ p>