问题
The goal of the following code is to save data in Cloud Firestore from a android device(api 24)
public class MainActivity extends AppCompatActivity {
public static final String NUM_KEY="num";
public static final String SMS_KEY="sms";
private static final String TAG = "MainActivity";
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("android/websms");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void saveSMS(View view){
EditText num = (EditText) findViewById(R.id.num);
EditText sms = (EditText) findViewById(R.id.msg);
String quotenum = num.getText().toString();
String quotesms = sms.getText().toString();
Button button = (Button) findViewById(R.id.button);
Map<String,Object> dataToSave = new HashMap<String, Object>();
dataToSave.put(NUM_KEY,quotenum);
dataToSave.put(SMS_KEY,quotesms);
mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Document has been saved");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG,"Document was not saved",e);
}
});
}
I'm following some Firestore tutorials on youtube and it seems to be a little bit outdated because when I'm running it on my android device I get the following error on the console
04-17 02:09:50.718 2600-2712/com.example.joao.websms1 E/art: The String#value field is not present on Android versions >= 6.0
Thank you in advance to anyone willing to help.
回答1:
¿Have u tried using the method push?
mDocRef.push(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Document has been saved");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG,"Document was not saved",e);
}
});
来源:https://stackoverflow.com/questions/49868406/unable-to-add-information-to-cloud-firestore