com.google.firebase.database.DatabaseException:无法将类型java.lang.String的值转换为double

com.google.firebase.database.DatabaseException:无法将类型java.lang.String的值转换为double

问题描述:

好的,所以这段代码一直有效,直到我的朋友决定更改Firebase结构,现在它不起作用.即使现在,如果我更改代码

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

这是错误代码:

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)

这是我的主要代码:

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) {

            }
        });

这是我的观看者代码:

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

这是我的适配器代码:

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

这是我的数据库结构:

我找不到错误,并且堆栈跟踪也没有帮助,因为它没有指出错误的代码.

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

------更新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 AND IT WORKED so i was curious and i tried another dataset but this time changing the parrent member to user like so

它不起作用 所以我的假设是Firebase适配器不喜欢 user 这个词,或者我的代码对Firebase适配器不喜欢 user 这个词做了什么,因为原始父级使用了 users

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

我在firebase中的数据是一个字符串,而不是locationLong和locationLat的双精度字,就像数据中的两个引号一样.

my data in my firebase was a string instead of a double for the locationLong and locationLat as evident as the 2 quotation marks in the data.

所以这个故事的寓意是检查您的变量

So the moral of the story here is check your variables

感谢评论部分中的所有帮助

Thanks for all the helps in the comment section