英文:
How to get logged user's information in drawer menu?
问题
我试图从 Firebase 中获取当前用户的用户名和电子邮件,并在抽屉菜单中显示出来。然而,程序崩溃并返回了以下错误:
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'
at com.example.android.myapplication.NavigationActivity$1.onDataChange(NavigationActivity.java:79)
以下是我的导航活动代码部分:
public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
// ... (其他代码)
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")) {
name.setText(child.getValue().toString());
}
if (child.getKey().equals("email")) {
email.setText(child.getValue().toString());
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// ... (其他代码)
}
这是我的数据库结构截图:
我猜测问题可能是因为 NavigationActivity
不是主要活动,然而,即使我将其更改为主要活动,我仍然遇到相同的错误。
英文:
I've tried to retrieve the current user's username and email from firebase and display it in the drawer menu. However, the program crashed and return the error:
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)
Here is my Navigation Activity
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;
}
}
Here is my database:
I guessed that this is because the NavigationActivity was not the main activity, however, even when I changed it to the main activity, I still got the same error.
答案1
得分: 2
你不需要使用 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")) {
name.setText(child.getValue().toString());
}
if (dataSnapshot.child("email").getValue(String.class).equals("email")) {
email.setText(child.getValue().toString());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
你的引用位于节点 userId
,因此要获取详细信息,你不需要进行迭代。
英文:
You dont need the for loop:
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) {
}
});
}
Your reference is at node userId
, therefore to get the details you dont need to iterate.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论