开发者社区> 问答> 正文

如何在抽屉式菜单中获取已记录的用户信息?

我试图从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

展开
收起
montos 2020-03-23 16:23:00 467 0
1 条回答
写回答
取消 提交回答
  • 您不需要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

    2020-03-23 16:30:50
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载