我已经尝试从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不是主要活动,但是,即使将其更改为主要活动,我仍然会遇到相同的错误。
您不需要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) {
}
});
}
您的引用在节点userId
上,因此不需要重复的详细信息。
这是Android版吗?如果没有,为什么要使用
Java