错误:com.google.firebase.database.DatabaseException:无法将类型java.util.ArrayList的值转换为String

时间:2018-10-11 22:08:43

标签: java android firebase firebase-realtime-database

我正在尝试在我的应用程序中查看几个图像,并且图像链接保存在Firebase实时数据库中。我将这些链接以.JSON格式上传,但没有手动将其添加到实时数据库中。尝试在我的应用程序中检索这些图像时,单击按钮以启动图像几秒钟后它崩溃(“ ZamCatalog已停止”)。在日志中,出现以下错误。不知道哪里出了问题,请帮忙。

  

E / Android运行时:致命异常:主要                     流程:com.example.android.zamcatalog,PID:13529                     com.google.firebase.database.DatabaseException:无法将类型java.util.ArrayList的值转换为String                         在com.google.android.gms.internal.zzelw.zzb(未知来源:93)                         在com.google.android.gms.internal.zzelw.zza(未知来源:0)                         位于com.google.firebase.database.DataSnapshot.getValue(未知来源:10)                         在com.example.android.zamcatalog.gameActivity $ 1.onDataChange(gameActivity.java:43)                         位于com.google.android.gms.internal.zzegf.zza(未知来源:13)                         在com.google.android.gms.internal.zzeia.zzbyc(未知来源:2)                         在com.google.android.gms.internal.zzeig.run(未知来源:71)                         在android.os.Handler.handleCallback(Handler.java:789)                         在android.os.Handler.dispatchMessage(Handler.java:98)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6944)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)   应用已终止。

下面是保存在数据库中的图像的快照

Firebase Realtime Database

gameActivity代码以检索图像

public class gameActivity extends AppCompatActivity {

ViewPager viewPager;
ViewPagerAdapter adapter;

private FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
private DatabaseReference reference=firebaseDatabase.getReference();
//private DatabaseReference childreference=reference.child("images");
private DatabaseReference gameReference=reference.child("game");

private ArrayList<String> images = new ArrayList<String>();

@Override
protected void onStart() {
    super.onStart();
    gameReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String images = dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
           // Log.w(TAG, "loadImage:onCancelled", databaseError.toException());

        }
    });
}

    viewPager = (ViewPager)findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(gameActivity.this,images);
    viewPager.setAdapter(adapter);
}

ViewPager适配器

public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
ArrayList<String> images;
LayoutInflater inflater;

public ViewPagerAdapter(Activity activity, ArrayList<String> images) {
    this.activity = activity;
    this.images = images;
}

@Override
public int getCount() {
    return images.size();
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
}

@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    inflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemview = inflater.inflate(R.layout.viewpager_item, container, false);

    ImageView image;
    image = (ImageView) itemview.findViewById(R.id.imageView);
    DisplayMetrics dis = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dis);
    int height = dis.heightPixels;
    int width = dis.widthPixels;
    image.setMinimumHeight(height);
    image.setMinimumWidth(width);

    try {
        Glide.with(activity.getApplicationContext())
                //.using(new FirebaseImageLoader())
                .load(images.get(position))  //This is were you put the storageReference
                .into(image);
    } catch (Exception ex) {

    }

    //notifyDataSetChanged();
    container.addView(itemview);
    return itemview;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

}

1 个答案:

答案 0 :(得分:0)

要正确获取所有这些网址,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameRef = rootRef.child("game");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getValue(String.class);
            Log.d("TAG", url);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
gameRef.addListenerForSingleValueEvent(valueEventListener);

logcat中的输出将是所有这些URL。