我是开发Android应用程序的初学者。我想做的功能是让用户能够将车牌添加到firebase-database中。我将使用ArrayList让用户将一个车牌列表添加到系统中。代码将显示如下:
List<String>carPlate=new ArrayList<String>();
mDatabase = FirebaseDatabase.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phone = Phone.getText().toString();
ic = IC.getText().toString();
fullname = FullName.getText().toString();
carplate = CarPlate.getText().toString();
carPlate.add(carplate);
//this data will be successful storing in firebase-database and the results will be the images above
Account account = new Account(fullname, phone, carPlate, ic);
if(user!=null) {
mDatabase.child(user.getUid()).setValue(account);
Toast.makeText(getBaseContext(), "Register Successfully...", Toast.LENGTH_LONG).show();
Intent in = new Intent(RegisterPage.this, MainActivity.class);
startActivity(in);
finish();
}
}
});
}
此外,另一个包含更新功能的类如下所示:
问题在这里
现在我想更新数据。根据这个Firebase数据库,我尝试map.get所有值,然后使用setter进行封装。之后,我从firebase-database中删除数据。然后我创建新对象,然后将所有任务放入其中。最后,我将setvalue设置为firebase-database。更新新数据后,firebase-database将自动推送到第二级。
当我再次更新时,它会再次推送
第二个问题是此行的NullException:phoneAcc.set_IC(map.get(“_ IC”));
错误
E/UncaughtException: java.lang.NullPointerException
at com.google.online_mobile_flexi_parking.ChangeEmailPassword$8$1.onDataChange(ChangeEmailPassword.java:251)
at com.google.android.gms.internal.zzakg.zza(Unknown Source)
at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source)
at com.google.android.gms.internal.zzalj$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5028)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
private List CarPlate;
changePhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
txtPhone = newPhone.getText().toString().trim();
if (!txtPhone.equals("")) {
mDatabase.child(user.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String key=mDatabase.child(user.getUid()).getKey();
Account phoneAcc=new Account();
Map<String,String>map=(Map)dataSnapshot.getValue();
phoneAcc.set_Key(mDatabase.child(user.getUid()).getKey());
phoneAcc.set_IC(map.get("_IC"));
phoneAcc.set_Phone(txtPhone);
phoneAcc.set_FullName(map.get("_FullName"));
CarPlate.add(map.get("_CarPlate0"));
phoneAcc.set_CarPlate0(CarPlate);
//dataSnapshot.getRef().removeValue();
mDatabase.child(mDatabase.child(user.getUid()).getKey()).removeValue();
Map<String,Object> task=new HashMap<String,Object>();
task.put("_FullName",phoneAcc.get_FullName());
task.put("_CarPlate0",phoneAcc.get_CarPlate0());
task.put("_Phone",phoneAcc.get_Phone());
task.put("_IC",phoneAcc.get_IC());
mDatabase.child(phoneAcc.get_Key()).setValue(task);
Toast.makeText(ChangeEmailPassword.this, "Phone Number is updated, sign in with new password!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ChangeEmailPassword.this, MainActivity.class));
finish();
signOut();
progressBar.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else if (txtPhone.equals("")) {
newPhone.setError("Enter phone number");
progressBar.setVisibility(View.GONE);
}
}
});