在Marathon Application中无法将类型java.lang.String的值转换为double值

时间:2019-04-04 16:56:21

标签: java android firebase firebase-realtime-database

我正在完成一个我正在从事的android应用程序项目。该应用程序是“分布式马拉松”,这意味着个人可以在不同位置相互竞争。但是,我的某些代码存在问题,我将收到错误:

com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to double

自昨晚以来,我一直在尝试解决此问题,但不幸的是没有成功。

我将在下面显示我认为相关的所有代码。如果需要其他代码,请告诉我。

Logcat:

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中,我收到此错误,将我定向到活动:

at com.aladdin.FitHub2019Project.userRacePage.UserRaceActivity$1.onDataChange(UserRaceActivity.java:116)

我所指导的课程是用户将与彼此进行比赛的竞赛活动。这个类有大约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; }
}

这是CustomAdapter:

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数据库结构:

Firebase Database Structure

1 个答案:

答案 0 :(得分:0)

要解决该错误,请使用以下命令:

    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类型来检索字段。