我想从我的 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/