java - 我想从 firebase 获取当前登录用户的名称

标签 java android firebase

我想从我的 Firebase 数据库获取用户名,但收到此错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

here is my firebase database structure:

下面是我的代码,它提供了相同的错误:

public class MyAccount extends AppCompatActivity {

private Button mSetupButton;
private EditText mSetupName;
private EditText mSetupBio;
private ImageButton mSetupImageButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String mPostKey=null;
private DatabaseReference mDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);

    mAuth=FirebaseAuth.getInstance();
    String user_id=mAuth.getCurrentUser().getUid();

    mDatabase= FirebaseDatabase.getInstance().getReference().child("Profiles");
    mPostKey=getIntent().getExtras().getString(user_id);

    mSetupName = (EditText) findViewById(R.id.accountName);
    mSetupBio = (EditText) findViewById(R.id.accountBio);
    mSetupButton = (Button) findViewById(R.id.accountButton);
    mSetupImageButton = (ImageButton) findViewById(R.id.accountImageButton);


    mDatabase.child(mPostKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String post_name = (String) dataSnapshot.child("Name").getValue();
            mSetupName.setText(post_name);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

}

显然错误是在获取用户 ID 时发生的。

最佳答案

只需根据您的命名约定和 FirstScreen 中的要求进行更新或添加(我几乎已经完成了您需要进行的所需更改)。

 Intent i = new Intent(FirstScreen.this, MyAccount.class);   
 String user_id;
 i.putExtra("user_id", user_id);

关于java - 我想从 firebase 获取当前登录用户的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520964/

相关文章:

java - 在 JTable 中自动添加一些文本

java - Jetty 在使用 http2 时以状态 200 而不是 304 响应

android - 如何使自定义 ListView 像附加图像一样

python - 使用 python Flask 进行 Firebase 匿名登录

java - 在 jackson 中禁用数字到字符串的自动转换

java - 子类型在用法上与子类有何不同?

java - 我如何检测是否在 Java libGDX 中触摸了 Sprite ?

java - 检索 Playstore 上我的应用程序列表

firebase - 如何在 Firebase 主机上部署 express 应用程序

ios - 使用 Firebase 查询 x 和 y 之间的项目