Android - 如何在firebase中添加新数据以退出唯一键?

时间:2017-07-05 04:22:21

标签: android firebase firebase-realtime-database

我想为名为" unique key"的子节点添加新数据。来自" pibref.push()。getkey"。当我首先尝试登录时,它完美无缺。 pic : my "dll dll" data inserted,  但如果我重新启动我的应用程序,我会收到如下错误:

07-05 11:14:46.341 11765-11765/com.rintan.geistr.pib W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
07-05 11:14:46.346 11765-11765/com.rintan.geistr.pib W/System.err:     at com.rintan.geistr.pib.transfer.vdigital(transfer.java:161)

public class SignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
    View.OnClickListener {
...
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);
    ...
    ...
    pibref = FirebaseDatabase.getInstance().getReference("Users");
    String key = pibref.push().getKey();

    Users = pibref.child(key); 
}
public static DatabaseReference userkey(){
    DatabaseReference userr = new SignInActivity().Users;
    return userr;
}

transfer.java

DatabaseReference UserKey = SignInActivity.userkey();
pibref_userkirim = UserKey.child("User_Transaksi");   //this is line 161
usr_tf = pibref_userkirim.child("Transfer");
usr_tf.child(stremailterima).setValue(transkirim);

请帮帮我......这是我的学期项目:'(

1 个答案:

答案 0 :(得分:0)

在onComplete listenre的覆盖方法中,执行以下操作:

User user=new User();
user.setFirstName(firstname);
user.setLastname(lastname);

  mDatabase.child("UserNode").child(auth.getCurrentUser().getUid()).setValue(user, new DatabaseReference.CompletionListener() {
  @Override
   public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
      if(databaseError==null)
          {
              Log.e("final value", "Data is saved successfully");
          }
                 }
                   });

在顶部声明此行:

 private DatabaseReference mDatabase;

希望有所帮助