Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

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

OK, so this code used to work until my friend decided to change the Firebase structure and now it doesn't work. Even now, if I change the code

Here's the error code :

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: mapp.com.sg.notymeclient, PID: 26480
                  com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to double
                      at com.google.android.gms.internal.zzbqi.zzaB(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zzc(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
                      at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6062)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6095)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5277)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3183)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                      at android.view.View.layout(View.java:17945)
                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)

and here's my main code :

mRef = FirebaseDatabase.getInstance().getReference()
                .child("users")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child("sent");


        mRecyclerView = (RecyclerView)view.findViewById(R.id.recycler_view_task);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new TaskAdapter(Task_GSON.class,R.layout.task_list_ui,TaskViewHolder.class,mRef);
        mRecyclerView.setAdapter(mAdapter);




        mRef.addValueEventListener(new ValueEventListener() {
            int index = 0 ;
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot eachTask : dataSnapshot.getChildren()) {
                    task.add(new Task_GSON());
                    for(DataSnapshot data : eachTask.getChildren()){
                        String dataString = data.getValue().toString();
                        switch (data.getKey()){
                            case "taskTitle":
                                task.get(index).setTaskTitle(dataString);
                                break;
                            case "taskDescription":
                                task.get(index).setTaskDescription(dataString);
                                break;
                            case "sendBy":
                                task.get(index).setSendBy(dataString);
                                break;
                            case "recieveBy":
                                task.get(index).setRecieveBy(dataString);
                                break;
                            case "locationLong":
                                task.get(index).setLocationLong( Double.parseDouble(dataString) );
                                break;
                            case "locationLat":
                                task.get(index).setLocationLat( Double.parseDouble(dataString) );
                                break;
                            case "locationName":
                                task.get(index).setLocationName(dataString);
                                break;
                            case "status":
                                task.get(index).setStatus(Integer.parseInt(dataString));
                                break;
                            case "time":
                                task.get(index).setTime(Long.parseLong(dataString));
                                break;

                        }
                    }
                    index++;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

and here's my viewholder code :

public class TaskViewHolder extends RecyclerView.ViewHolder{
    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewTime;
    TextView textViewTaskAssign;
    public TaskViewHolder(View itemView) {
        super(itemView);
        textViewTitle = (TextView)itemView.findViewById(R.id.text_view_title);
        textViewDescription = (TextView)itemView.findViewById(R.id.text_view_task_description);
        textViewTime = (TextView)itemView.findViewById(R.id.text_view_time);
        textViewTaskAssign = (TextView)itemView.findViewById(R.id.text_view_task_assign);
    }
}

and here's my adapter codes:

public class TaskAdapter extends FirebaseRecyclerAdapter<Task_GSON,TaskViewHolder> {
    public TaskAdapter(Class<Task_GSON> modelClass, int modelLayout, Class<TaskViewHolder> viewHolderClass, DatabaseReference ref) {
        super(modelClass, modelLayout, viewHolderClass, ref);
    }

    @Override
    protected void populateViewHolder(TaskViewHolder viewHolder, Task_GSON model, int position) {
        if(model != null){
            viewHolder.textViewDescription.setText(model.getTaskDescription());
            viewHolder.textViewTitle.setText(model.getTaskTitle());
            android.text.format.DateFormat df = new android.text.format.DateFormat();
            String reminderDate = df.format("dd/MM/yy hh:mm a" , model.getTime()).toString();
            viewHolder.textViewTime.setText(reminderDate);
            viewHolder.textViewTaskAssign.setText("send to:" +model.getRecieveBy());
        }
    }
}

and here's my database structure:

enter image description here

I couldn't find the error and the stacktrace doesn't help as it didn't pin point the code that mess up.

------UPDATE 1--------

ok so I was curious if my code can actually view the data or not and i made a few tested myself so first of i tried changing the database structure to something like this enter image description here AND IT WORKED so i was curious and i tried another dataset but this time changing the parrent member to user like so

change member to user

and it doesn't work so my hypothesis is that somehow either firebase adapter hates the word user or my code is doing something to firebase adapter to hate the word user as the original parent takes users

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...