我正在做一个使用Firebase Firestore存储数据的Android项目。我试图从Firestore数据库中检索一个文档,并获取一个名为“停留时间”的字段的值,该字段是一个数字,但我一直得到以下错误:
java.lang.RuntimeException:字段“驻留时间”不是java.lang.Number
我使用getLong()方法来检索字段的值,但似乎该字段未被识别为数字。下面是我使用的代码:
package com.example.projetm.data;
import static java.sql.DriverManager.println;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVSaver;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.FastVector;
import weka.core.Instances;
public class Getdata {
FirebaseFirestore db = FirebaseFirestore.getInstance();
private SharedPreferences preferences;
private String PREFS = "KEYSTROCKES";
private int nombre_instance; // hada nombre de tests
private int nombre_attribute = 29; // hada nombre de collones
public ArrayList dwell = new ArrayList();
public ArrayList flightm2 = new ArrayList();
public ArrayList flightm3 = new ArrayList();
ArrayList name = new ArrayList<String>();
int y=0;
public String Class(Context context) throws Exception {
//hna nkharej ga3 les utilisateurs
db.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
int nbruser = task.getResult().size(); //hna nedi nombre des utilisateurs
Log.d("nombre",String.valueOf(nbruser));
for (QueryDocumentSnapshot doc : task.getResult()) { //apres nboucle les utilisateurs
String id = doc.getId(); // nedi id ta3ah
//apres nedi dynamique regle de chaque utilisateurs
db.collection("users").document(id).collection("dynamic").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
y++; // w koul ma nedi test nzid f y
Log.d("getdata1", String.valueOf(nombre_instance));
nombre_instance+=task.getResult().size(); //w nmed l nombre instance le nombre de test
for (QueryDocumentSnapshot document : task.getResult()) { //apres nboucle les tests
Log.d("data type", document.get("dwell time").getClass().getSimpleName());
dwell.add(document.getLong("dwell time")); //nedi swalhah
flightm2.add(document.getLong("flight time 2"));
flightm3.add(document.getLong("flight time 3"));
name.add(document.getString("nom"));
Log.d("getdata1", "y = " + y);
}
if (y==nbruser){ //apres ki y yweli 9ad le nombre des utilisateurs sema da ga3 les tests ydir la suite
Log.d("getdata", "wsel");
//hna apres ncreeo data
preferences = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
//nsamo les atributs
final Attribute Attribute1 = new Attribute("dwelltime1");
final Attribute Attribute2 = new Attribute("dwelltime2");
final Attribute Attribute3 = new Attribute("dwelltime3");
final Attribute Attribute4 = new Attribute("dwelltime4");
final Attribute Attribute5 = new Attribute("dwelltime5");
final Attribute Attribute6 = new Attribute("dwelltime6");
final Attribute Attribute7 = new Attribute("dwelltime7");
final Attribute Attribute8 = new Attribute("dwelltime8");
final Attribute Attribute9 = new Attribute("dwelltime9");
final Attribute Attribute10 = new Attribute("dwelltime10");
final Attribute Attribute11 = new Attribute("flighttime2_1");
final Attribute Attribute12 = new Attribute("flighttime2_2");
final Attribute Attribute13 = new Attribute("flighttime2_3");
final Attribute Attribute14 = new Attribute("flighttime2_4");
final Attribute Attribute15 = new Attribute("flighttime2_5");
final Attribute Attribute16 = new Attribute("flighttime2_6");
final Attribute Attribute17 = new Attribute("flighttime2_7");
final Attribute Attribute18 = new Attribute("flighttime2_8");
final Attribute Attribute19 = new Attribute("flighttime2_9");
final Attribute Attribute20 = new Attribute("flighttime3_1");
final Attribute Attribute21 = new Attribute("flighttime3_2");
final Attribute Attribute22 = new Attribute("flighttime3_3");
final Attribute Attribute23 = new Attribute("flighttime3_4");
final Attribute Attribute24 = new Attribute("flighttime3_5");
final Attribute Attribute25 = new Attribute("flighttime3_6");
final Attribute Attribute26 = new Attribute("flighttime3_7");
final Attribute Attribute27 = new Attribute("flighttime3_8");
final Attribute Attribute28 = new Attribute("flighttime3_9");
List<String> classes = new ArrayList<String>(){
{
add("sahraoui");
}
}; //w ndir les classe
//apres n ajoutou les attributs f fastvecteur
Attribute AttributeClass = new Attribute("class",classes);
FastVector fastVector = new FastVector(nombre_attribute);
fastVector.addElement(Attribute1);
fastVector.addElement(Attribute2);
fastVector.addElement(Attribute3);
fastVector.addElement(Attribute4);
fastVector.addElement(Attribute5);
fastVector.addElement(Attribute6);
fastVector.addElement(Attribute7);
fastVector.addElement(Attribute8);
fastVector.addElement(Attribute9);
fastVector.addElement(Attribute10);
fastVector.addElement(Attribute11);
fastVector.addElement(Attribute12);
fastVector.addElement(Attribute13);
fastVector.addElement(Attribute14);
fastVector.addElement(Attribute15);
fastVector.addElement(Attribute16);
fastVector.addElement(Attribute17);
fastVector.addElement(Attribute18);
fastVector.addElement(Attribute19);
fastVector.addElement(Attribute20);
fastVector.addElement(Attribute21);
fastVector.addElement(Attribute22);
fastVector.addElement(Attribute23);
fastVector.addElement(Attribute24);
fastVector.addElement(Attribute25);
fastVector.addElement(Attribute26);
fastVector.addElement(Attribute27);
fastVector.addElement(Attribute28);
fastVector.addElement(AttributeClass);
//apres ncrreo instance avec nom dynamic w nmedo le nombre d'instance
Instances instances = new Instances("dynamic",fastVector,nombre_instance);
instances.setClassIndex(instances.numAttributes()-1);
ArrayList finalDwell = dwell;
ArrayList finalFlightm2 = flightm2;
ArrayList finalFlightm3 = flightm3;
Log.d("taille", String.valueOf(finalDwell.size()));
for (int i=0 ; i <nombre_instance;i++) { //apres nboucle pour chaque instance (test)
int finalI = i; // i hiiya instance li rana fiha
DenseInstance denseInstance = new DenseInstance(instances.numAttributes()) {
{
setValue(Attribute1, preferences.getInt("dwelltime1",(Integer) finalDwell.get(0)));
setValue(Attribute2, preferences.getLong("dwelltime2", (Long) finalDwell.get(1)));
setValue(Attribute3, preferences.getLong("dwelltime3", (Long) finalDwell.get(2)));
setValue(Attribute4, preferences.getLong("dwelltime4", (Long) finalDwell.get(3)));
setValue(Attribute5, preferences.getLong("dwelltime5", (Long) finalDwell.get(4)));
setValue(Attribute6, preferences.getLong("dwelltime6", (Long) finalDwell.get(5)));
setValue(Attribute7, preferences.getLong("dwelltime7", (Long) finalDwell.get(6)));
setValue(Attribute8, preferences.getLong("dwelltime8", (Long) finalDwell.get(7)));
setValue(Attribute9, preferences.getLong("dwelltime9", (Long) finalDwell.get(8)));
setValue(Attribute10, preferences.getLong("dwelltime10", (Long) finalDwell.get(9)));
setValue(Attribute11, preferences.getLong("flighttime2_1", (Long) finalFlightm2.get(0)));
setValue(Attribute12, preferences.getLong("flighttime2_2", (Long) finalFlightm2.get(1)));
setValue(Attribute13, preferences.getLong("flighttime2_3", (Long) finalFlightm2.get(2)));
setValue(Attribute14, preferences.getLong("flighttime2_4", (Long) finalFlightm2.get(3)));
setValue(Attribute15, preferences.getLong("flighttime2_5", (Long) finalFlightm2.get(4)));
setValue(Attribute16, preferences.getLong("flighttime2_6", (Long) finalFlightm2.get(5)));
setValue(Attribute17, preferences.getLong("flighttime2_7", (Long) finalFlightm2.get(6)));
setValue(Attribute18, preferences.getLong("flighttime2_8", (Long) finalFlightm2.get(7)));
setValue(Attribute19, preferences.getLong("flighttime2_9", (Long) finalFlightm2.get(8)));
setValue(Attribute20, preferences.getLong("flighttime3_1", (Long) finalFlightm3.get(0)));
setValue(Attribute21, preferences.getLong("flighttime3_2", (Long) finalFlightm3.get(1)));
setValue(Attribute22, preferences.getLong("flighttime3_3", (Long) finalFlightm3.get(2)));
setValue(Attribute23, preferences.getLong("flighttime3_4", (Long) finalFlightm3.get(3)));
setValue(Attribute24, preferences.getLong("flighttime3_5", (Long) finalFlightm3.get(4)));
setValue(Attribute25, preferences.getLong("flighttime3_6", (Long) finalFlightm3.get(5)));
setValue(Attribute26, preferences.getLong("flighttime3_7", (Long) finalFlightm3.get(6)));
setValue(Attribute27, preferences.getLong("flighttime3_8", (Long) finalFlightm3.get(7)));
setValue(Attribute28, preferences.getLong("flighttime3_9", (Long) finalFlightm3.get(8)));
}
};
instances.add(denseInstance); //apres n ajouter les donnes f instance
}
// Create a file to save the CSV data to
File file = new File(context.getExternalFilesDir(null), "data.arff");
try (FileWriter writer = new FileWriter(file)) {
// Save the CSV data to the file
// create ArffSaver object
ArffSaver saver = new ArffSaver();
// set the instances object to be saved
saver.setInstances(instances);
// set the file to save the instances
saver.setFile(file);
// save the instances to the ARFF file
saver.writeBatch();
Log.d("APP_TAG", "ARFF file saved at: " + file.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.d("getdata", instances.toString()); // w n affichi les donnees f log
}
}
}
});
}
}
}
});
return "fdg";
}
}
and this a screenshot of my Firestore database
我已经验证了“停留时间”字段确实是Firestore中的数字字段,所以我不知道为什么会出现这个错误。是什么导致了这个错误,以及如何修复它?
1条答案
按热度按时间m1m5dgzv1#
我已经验证了“停留时间”字段确实是Firestore中的数字字段,所以我不知道为什么会出现这个错误。是什么导致了这个错误,以及如何修复它?
但是你的数据库的截图显示'停留时间'字段是一个数组而不是一个数字,只要检查你是否设置了'停留时间'字段的权利。