【问题标题】:Attempt to invoke interface method com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged' on a null object reference尝试在空对象引用上调用接口方法 com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged'
【发布时间】:2019-06-26 04:35:50
【问题描述】:

应用在查看详细信息时被强制停止

我尝试了不同的方法来连接到我的 firebase 用户 ID,但它给出了空指针异常并显示了调用接口方法的尝试 Firebase Auth 状态监听器抛出错误

查看详情.class

public class ViewDetails extends AppCompatActivity {

    private DatabaseReference myref;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseDatabase mFirebaseDatabase;
    private String userId;

    ListView listView;

    private TextView name,email,phone,shopname,shopaddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_details);
        setContentView(R.layout.activity_login);getWindow().setBackgroundDrawableResource(R.drawable.inventorybackground);

        listView=(ListView)findViewById(R.id.listview);
        name=(TextView)findViewById(R.id.name);
        email=(TextView)findViewById(R.id.email);
        phone=(TextView)findViewById(R.id.phonenumber);
        shopname=(TextView)findViewById(R.id.shopname);
        shopaddress=(TextView)findViewById(R.id.shopaddress);

        mAuth=FirebaseAuth.getInstance();
        mFirebaseDatabase=FirebaseDatabase.getInstance();
        myref=mFirebaseDatabase.getReference();
        FirebaseUser user=mAuth.getCurrentUser();
        userId=user.getUid();

        myref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                showData(dataSnapshot);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void showData(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds:dataSnapshot.getChildren())
        {
            getdetails get=new getdetails();
            get.setName(ds.child(userId).getValue(getdetails.class).getName());
            get.setEmail(ds.child(userId).getValue(getdetails.class).getEmail());
            get.setPhone(ds.child(userId).getValue(getdetails.class).getPhone());
            get.setShopname(ds.child(userId).getValue(getdetails.class).getShopname());
            get.setShopadd(ds.child(userId).getValue(getdetails.class).getShopadd());

            ArrayList<String> array = new ArrayList<>();
            array.add(get.getName());
            array.add(get.getEmail());
            array.add(get.getPhone());
            array.add(get.getShopname());
            array.add(get.getShopadd());

            ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
            listView.setAdapter(adapter);
        }
    }

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

    @Override
    protected void onStop() {
        super.onStop();
        if(mAuthListener!=null)
        {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
    private void toastMessage(String message)
    {
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
    }
}

怎么了?

【问题讨论】:

    标签: android firebase-realtime-database firebase-authentication


    【解决方案1】:

    因为mAuthListener 从未实例化,所以您将得到 null。

    在您的 onCreate() 中执行此操作

    mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                    if (user != null) {
                        // Your logic
                    }
                }
            };
    

    【讨论】:

    • 我已经登录并获取用户详细信息,但是通过进行活动它让我自动注销 idk 为什么使用 getCurrentUser();
    • 这个答案是否解决了您关于 NullPointerException 的问题?如果是,请将此问题标记为正确并用您的其他问题打开另一个问题
    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2018-11-21
    相关资源
    最近更新 更多