我试图从Firebase检索当前用户的用户名和电子邮件,并将其显示在抽屉菜单中。但是,程序崩溃并返回错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.android.myapplication.NavigationActivity$1.onDataChange(NavigationActivity.java:79)
这是我的导航活动
public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
protected DrawerLayout drawer;
private String userId;
private TextView email,name;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
name = (TextView)findViewById(R.id.name);
email=(TextView)findViewById(R.id.email);
mAuth=FirebaseAuth.getInstance();
userId= mAuth.getCurrentUser().getUid();
getUserInformation();
}
private void getUserInformation() {
DatabaseReference userDatabase= FirebaseDatabase.getInstance().getReference().child("users").child(userId);
userDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
if (child.getKey().equals("name")) {//find the child that Equals to start
name.setText(child.getValue().toString());
}
if (child.getKey().equals("email")) {//find the child that Equals to start
email.setText(child.getValue().toString());
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_run:
Intent intent = new Intent(NavigationActivity.this,running_map.class);
startActivity(intent);
finish();
break;
case R.id.nav_history:
Intent intent2 = new Intent(NavigationActivity.this,HistoryActivity.class);
startActivity(intent2);
finish();
break;
//......
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我猜想这是因为NavigationActivity不是主要活动,但是即使将其更改为主要活动,我仍然遇到相同的错误。
问题来源:Stack Overflow
您不需要for循环:
private void getUserInformation() {
DatabaseReference userDatabase= FirebaseDatabase.getInstance().getReference().child("users").child(userId);
userDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.child("name").getValue(String.class).equals("name")) {//find the child that Equals to start
name.setText(child.getValue().toString());
}
if (dataSnapshot.child("email").getValue(String.class).equals("email")) {//find the child that Equals to start
email.setText(child.getValue().toString());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
您的参考资料位于node userId,因此无需重复即可获取详细信息。
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。