无法向Cloud Firestore添加信息

时间:2018-04-17 01:18:33

标签: android firebase google-cloud-firestore

以下代码的目标是从Android设备(api 24)中保存Cloud Firestore中的数据

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);
            }
        });

}

我在youtube上关注了一些Firestore教程,它似乎有点过时,因为当我在我的Android设备上运行它时,我在控制台上出现以下错误

04-17 02:09:50.718 2600-2712 / com.example.joao.websms1 E / art:Android版本中不存在String#value字段&gt; = 6.0

提前感谢愿意提供帮助的任何人。

1 个答案:

答案 0 :(得分:0)

¿你尝试过使用方法推送吗?

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);
        }
    });