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

时间:2017-09-12 12:08:10

标签: android firebase firebase-realtime-database type-conversion

我的Blog课程在这里

package com.nego.lakshan.nego;


public class Blog {

private String FROM;
private String TO;
private String DESCRIPTION;
private String IMAGE;
private String DATE;
private double TOLAT;
private double TOLNG;
private double FROMLAT;
private double FROMLNG;

public Blog(){

}

public Blog(String FROM, String TO, String DESCRIPTION, String IMAGE, String DATE, String TOLAT, String TOLNG, String FROMLAT, String FROMLNG) {
    this.FROM = FROM;
    this.TO = TO;
    this.DESCRIPTION = DESCRIPTION;
    this.IMAGE = IMAGE;
    this.DATE = DATE;
    this.TOLAT = Double.parseDouble(TOLAT);
    this.TOLNG = Double.parseDouble(TOLNG);
    this.FROMLAT = Double.parseDouble(FROMLAT);
    this.FROMLNG = Double.parseDouble(FROMLNG);
}



public String getFROM() {
    return FROM;
}

public void setFROM(String FROM) {
    this.FROM = FROM;
}

public String getTO() {
    return TO;
}

public void setTO(String TO) {
    this.TO = TO;
}

public String getIMAGE() {
    return IMAGE;
}

public void setIMAGE(String IMAGE) {
    this.IMAGE = IMAGE;
}

public String getDATE() {
    return DATE;
}

public void setDATE(String DATE) {
    this.DATE = DATE;
}

public double getTOLAT() {
    return TOLAT;
}

public void setTOLAT(String TOLAT) {
    this.TOLAT = Double.parseDouble(TOLAT);
}

public double getTOLNG() {
    return TOLNG;
}

public void setTOLNG(String TOLNG) {
    this.TOLNG = Double.parseDouble(TOLNG);
}

public double getFROMLAT() {
    return FROMLAT;
}

public void setFROMLAT(String FROMLAT) {
    this.FROMLAT = Double.parseDouble(FROMLAT);
}

public double getFROMLNG() {
    return FROMLNG;
}

public void setFROMLNG(String FROMLNG) {
    this.FROMLNG = Double.parseDouble(FROMLNG);
}


public String getDESCRIPTION() {
    return DESCRIPTION;
}

public void setDESCRIPTION(String DESCRIPTION) {
    this.DESCRIPTION = DESCRIPTION;
}

}

和我的Fragment类用于查看我从运行时数据库(Firebase)获取的回收器视图中的数据。 我的片段类在这里

package com.nego.lakshan.nego;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;




public class RecieverFragment extends Fragment {


public RecieverFragment() {
    // Required empty public constructor
}

private RecyclerView mBlogList;
private DatabaseReference mDatabaseReference;
private View v;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v=inflater.inflate(R.layout.fragment_reciever, container, false);


    mBlogList= (RecyclerView) v.findViewById(R.id.fragment);


    mBlogList.setHasFixedSize(true);
    mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Blog");





    mBlogList.setLayoutManager(new LinearLayoutManager(getContext()));



    Toast.makeText(getContext(),"mmmm",Toast.LENGTH_SHORT).show();
    return v;





}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Blog,BlogViewHolder> 
 mFirebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, BlogViewHolder>
(Blog.class,
            R.layout.blog_item,
            BlogViewHolder.class,
            mDatabaseReference) {
        @Override
        protected void populateViewHolder(BlogViewHolder viewHolder, Blog 
 model, int position) {


            Toast.makeText(getContext(),"maaa",Toast.LENGTH_SHORT).show();


            viewHolder.setDATE(model.getDATE());
            viewHolder.setDESCRIPTION(model.getDESCRIPTION());
            viewHolder.setFROM(model.getFROM());
            viewHolder.setTO(model.getTO());




        }
    };
    mBlogList.setAdapter(mFirebaseRecyclerAdapter);
}

public static class BlogViewHolder extends  RecyclerView.ViewHolder{

    View mView;

    public BlogViewHolder(View itemView) {
        super(itemView);
        mView=itemView;}
    public void setDATE(String DATE){
        TextView date = (TextView) mView.findViewById(R.id.DATE);
        date.setText(DATE);
    }

    public void setDESCRIPTION(String DESCRIPTION){
        TextView desc= (TextView) mView.findViewById(R.id.DESCRIPTION);
        desc.setText(DESCRIPTION);
    }

    public void setFROM(String FROM){
        TextView from = (TextView) mView.findViewById(R.id.FROM);
        from.setText(FROM);
    }

    public void setTO(String TO){
        TextView to = (TextView) mView.findViewById(R.id.TO);
        to.setText(TO);
    }

    }
}

和我的片段布局文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nego.lakshan.nego.RecieverFragment"
android:id="@+id/place_holder">

<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment">
</android.support.v7.widget.RecyclerView>


</FrameLayout>

我正在使用CardView进行查看(博客文章)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="104dp"
android:orientation="horizontal"
android:weightSum="1">

<ImageView
    android:id="@+id/PROFILEPIC"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1.05" />

<LinearLayout
    android:layout_width="244dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/NAME"
        android:layout_width="match_parent"
        android:layout_height="53dp" />

    <TextView
        android:id="@+id/DATE"
        android:layout_width="match_parent"
        android:layout_height="52dp" />
</LinearLayout>

</LinearLayout>



    <TextView
        android:id="@+id/DESCRIPTION"
        android:layout_width="match_parent"
        android:layout_height="164dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.16"
            android:weightSum="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="From"
                android:layout_weight="0.31" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:text="To"
                android:layout_weight="0.51" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.88"
            android:weightSum="1">

            <TextView
                android:id="@+id/FROM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.41" />

            <TextView
                android:id="@+id/TO"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.19" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_width="109dp"
                android:layout_height="wrap_content"
                android:text="FROMLOCATION"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TOLOCATION"/>


        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="164dp"
            android:layout_height="wrap_content"
            android:text="DIrection"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Message"/>
    </LinearLayout>


</LinearLayout>


</android.support.v7.widget.CardView>

并且我收到错误,当我调用片段时应用程序崩溃

com.nego.lakshan.nego E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: 
com.nego.lakshan.nego, PID: 28426

com.google.firebase.database.DatabaseException: Failed to convert value of 
type java.lang.Double to String
                                                                       at 
com.google.android.gms.internal.zzbqi.zzaD(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(FirebaseRecycler
Adapter.java:163)
                                                                       at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:152)
                                                                       at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:195)
                                                                       at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
                                                                       at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                       at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1648)
                                                                       at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:343)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:549)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5319)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
请帮助我。

2 个答案:

答案 0 :(得分:0)

问题出在Blog.class中。当您将以下变量声明为double时,您的类构造函数正在接受字符串来初始化它:

private double TOLAT;
private double TOLNG;
private double FROMLAT;
private double FROMLNG;

现在问题是Firebase数据库会根据此处的定义和“获取”来保存它。方法,如Double。但是当他们检索博客时,从类构造函数的数据库中,它无法将其转换为String。

请将您的变量类型修复为一致的声明,此错误应该消失。

答案 1 :(得分:0)

我添加了一些值来测试,我忘了将它们从firebase实时数据库中删除..后来我意识到并删除它们然后它工作了,谢谢