我正在完成一个我一直在从事的 Android 应用程序项目。该应用程序是一场“分布式马拉松”,意味着个人可以在不同地点相互比赛。但是,我的一些代码有问题,我会收到错误:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to double
从昨晚开始我一直在尝试解决这个问题,但不幸的是没有成功。
我将在下面显示我认为相关的所有代码,如果需要其他代码,请告诉我。
日志猫:
2019-04-04 16:41:15.230 29421-29421/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aladdin.FitHub2019Project, PID: 29421
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to double
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertDouble(com.google.firebase:firebase-database@@16.1.0:395)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database@@16.1.0:276)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:197)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@16.1.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@16.1.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:591)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:550)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.1.0:420)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
at com.aladdin.FitHub2019Project.userRacePage.UserRaceActivity$1.onDataChange(UserRaceActivity.java:116)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
在 Logcat 中,我收到此错误,将我引导至某个 Activity :
at com.aladdin.FitHub2019Project.userRacePage.UserRaceActivity$1.onDataChange(UserRaceActivity.java:116)
我所针对的类(class)是用户将相互竞赛的竞赛 Activity 。这个类有大约 600 多行代码,但我收到的错误是在 OnCreate 中 - 因此我只会发布这部分代码。注释“//ERROR”是我转到的行:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_race);
timer = new Stopwatch();
name=findViewById(R.id.nameTV);
timerTV=findViewById(R.id.timerTV);
distanceTV=findViewById(R.id.distanceTV);
hourET=findViewById(R.id.hourET);
minET=findViewById(R.id.minET);
startBtn=findViewById(R.id.startBtn);
pauseBtn =findViewById(R.id.pauseBtn);
resetBtn=findViewById(R.id.resetBtn);
logoutBtn=findViewById(R.id.logoutBtn);
setTimeBtn=findViewById(R.id.setTimeBtn);
databaseReference= FirebaseDatabase.getInstance().getReference("RunningUsers");
startBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
resetBtn.setOnClickListener(this);
setTimeBtn.setOnClickListener(this);
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
listView=findViewById(R.id.RunningListView);
Query dr=FirebaseDatabase.getInstance().getReference("RunningUsers").orderByChild("totalSec");
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userNameArrayList.clear();userStatusArrayList.clear();userTimerArrayList.clear();userDistanceArrayList.clear();
for (DataSnapshot s:dataSnapshot.getChildren())
{
RunningUserModel rUM=s.getValue(RunningUserModel.class); //ERROR
userNameArrayList.add(rUM.getUserName());
userStatusArrayList.add(rUM.getUserStatus());
userTimerArrayList.add(rUM.getUserTimer());
userDistanceArrayList.add(rUM.getUserDistance());
}
Collections.reverse(userNameArrayList);Collections.reverse(userStatusArrayList);Collections.reverse(userTimerArrayList);
Collections.reverse(userDistanceArrayList);
customAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"error while reading",Toast.LENGTH_SHORT).show();
}
});
RunningUserModel 的代码如下:
public class RunningUserModel {
String userName,userStatus,userTimer;
double totalSec;
double userDistance;
public RunningUserModel()
{
}
public RunningUserModel(String userName, String userStatus, String userTimer, double userDistance, double totalSec) {
this.userName = userName;
this.userStatus = userStatus;
this.userTimer = userTimer;
this.userDistance = userDistance;
this.totalSec=totalSec;
}
public String getUserName() {
return userName;
}
public String getUserStatus() {
return userStatus;
}
public String getUserTimer() {
return userTimer;
}
public double getUserDistance() {
return userDistance;
}
public double getTotalSec() {return totalSec; }
}
这是自定义适配器:
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.aladdin.FitHub2019Project.R;
import java.util.ArrayList;
public class CustomAdapterUserRace extends ArrayAdapter{
LayoutInflater layoutInflater;
TextView userNameTV, userStatusTV, userTimerTV, userDistanceTV;
Activity activity;
ArrayList<String> userName =new ArrayList<String>();
ArrayList<String> userStatus =new ArrayList<String>();
ArrayList<String> userTimer =new ArrayList<String>();;
ArrayList<Double> userDistance =new ArrayList<Double>();
public CustomAdapterUserRace(@NonNull Activity activity, ArrayList<String> userName, ArrayList<String>userStatus, ArrayList<String> userTimer, ArrayList<Double> userDistance)
{
super(activity, R.layout.runninglistener, userName);
this.activity=activity;
this.userName =userName;
this.userStatus =userStatus;
this.userTimer =userTimer;
this.userDistance =userDistance;
layoutInflater=activity.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView=layoutInflater.inflate(R.layout.runninglistener,null);
userNameTV =convertView.findViewById(R.id.userName);
userStatusTV =convertView.findViewById(R.id.userStatus);
userTimerTV =convertView.findViewById(R.id.usertimer);
userDistanceTV =convertView.findViewById(R.id.userDistance);
userNameTV.setText(userName.get(position));
userStatusTV.setText(userStatus.get(position));
userTimerTV.setText(userTimer.get(position));
userDistanceTV.setText(String.valueOf(userDistance.get(position)));
return convertView;
}
}
Firebase 数据库结构:
最佳答案
要解决该错误,请使用以下命令:
Query dr=FirebaseDatabase.getInstance().getReference("RunningUsers").orderByChild("totalSec");
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userNameArrayList.clear();userStatusArrayList.clear();userTimerArrayList.clear();userDistanceArrayList.clear();
RunningUserModel rUM = dataSnapshot.getValue(RunningUserModel.class); //ERROR
userNameArrayList.add(rUM.getUserName());
userStatusArrayList.add(rUM.getUserStatus());
userTimerArrayList.add(rUM.getUserTimer());
userDistanceArrayList.add(rUM.getUserDistance());
Collections.reverse(userNameArrayList);Collections.reverse(userStatusArrayList);Collections.reverse(userTimerArrayList);
Collections.reverse(userDistanceArrayList);
customAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"error while reading",Toast.LENGTH_SHORT).show();
}
});
删除 for 循环,因为当您进行迭代时,您将以 String
类型而不是 RunningUserModel
类型检索字段。
关于java - 无法在 Marathon 应用程序中将 java.lang.String 类型的值转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55521318/