gpt4 book ai didi

android - 尝试在空对象引用上调用接口(interface)方法 com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged'

转载 作者:行者123 更新时间:2023-11-30 05:06:31 27 4
gpt4 key购买 nike

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

我尝试了不同的方法来连接到我的 firebase 用户 ID,但它给出了空指针异常并显示了调用接口(interface)方法的尝试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();
}
}

怎么了?

最佳答案

您得到的是 null,因为 mAuthListener 从未被实例化。

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

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

关于android - 尝试在空对象引用上调用接口(interface)方法 com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482224/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com