我无法解决现在刚出现的这个问题......一切都运转良好......这就是错误:
com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.nicco.wallpapersapp.GridItem类型 在com.google.android.gms.internal.zzbtg.zze(未知来源) 在com.google.android.gms.internal.zzbtg.zzb(未知来源) 在com.google.android.gms.internal.zzbtg.zza(未知来源) 在com.google.firebase.database.DataSnapshot.getValue(未知来源) 在com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151) 在com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140) 在com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183) 在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6482) 在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6515) 在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458) 在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724) 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5563) 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5559) 在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2229) 在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556) 在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) 在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) 在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) 在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) 在android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710) 在android.support.v7.widget.RecyclerView $ 1.run(RecyclerView.java:346) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874) 在android.view.Choreographer.doCallbacks(Choreographer.java:686) 在android.view.Choreographer.doFrame(Choreographer.java:618) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 申请已终止。
我的班级:
public class HomePage extends AppCompatActivity {
private DrawerLayout mDraw;
private android.support.v7.app.ActionBarDrawerToggle mToggle;
private DatabaseReference mDatabase;
private RecyclerView recycler;
private Toolbar mToolbar;
private NavigationView nView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
mDatabase = FirebaseDatabase.getInstance().getReference("landscape");
recycler = findViewById(R.id.recyclerView);
recycler.setLayoutManager(new LinearLayoutManager(this));
//layoutManager = new GridLayoutManager(this,2);
//recycler.setLayoutManager(layoutManager);
recycler.setHasFixedSize(true);
mToolbar = findViewById(R.id.toolbarNature);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Landscape Wallpapers");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDraw = findViewById(R.id.drawerLayout);
mToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDraw, mToolbar, R.string.open, R.string.close);
mDraw.addDrawerListener(mToggle);
mToggle.syncState();
nView = findViewById(R.id.navigViewNature);
nView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.landscape_area){
startActivity(getIntent());
}
if(id == R.id.wallMoment){
startActivity(new Intent(HomePage.this, AppMoment.class));
}
if(id == R.id.minimal_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.city_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.mountain_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.beach_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.sea_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.cars_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
if(id == R.id.bikes_area){
//startActivity(new Intent(AppMoment.this, AppMoment.class));
}
return false;
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<GridItem, MyViewHoler> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<GridItem, MyViewHoler>(
GridItem.class,
R.layout.gridview_item_layout,
MyViewHoler.class,
mDatabase
) {
@Override
protected void populateViewHolder(MyViewHoler viewHolder, GridItem model, int position) {
final String imageData = getRef(position).getKey();
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
recycler.setAdapter(firebaseRecyclerAdapter);
recycler.setHasFixedSize(true);
}
public static class MyViewHoler extends RecyclerView.ViewHolder {
View mView;
public MyViewHoler(View itemView) {
super(itemView);
mView = itemView;
}
public void setImage(Context ctx, String image){
ImageView mImage = mView.findViewById(R.id.item_image);
//Picasso.with(ctx).load(image).into(mImage);
Glide.with(ctx).load(image).into(mImage);
}
}
}
我的GridItem类:
public class GridItem {
private String image;
public GridItem(){
}
public GridItem(String image) {
this.image = image;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
这是数据库:
wallpapaersapp
-beach
-bikes
-cars
-city
-landscape
image: "https://firebasestorage.googleapis.com/v0/b/wal..."
-minimal
-mountain
-sea
-wallmoment
答案 0 :(得分:0)
FirebaseUI正在执行您的查询,以查找要显示的GridItem
下的子项列表。您的代码假设每个子项都可以映射到-landscape
- child1
- image: "https://firebasestorage.googleapis.com/v0/b/wal..."
- child2
- image: "https://firebasestorage.googleapis.com/v0/b/wal..."
类型。
但是,现在,您的位置只有一个子元素,其值为String。单个String无法映射到Java对象。这就是你的错误来自哪里。更改数据库的结构以添加可以映射到数据模型的额外级别的子对象,或将FirebaseUI类型映射更改为String。
如果您想使用GridItem,您的数据库应如下所示:
CREATE Table #tempTable
(
Place VARCHAR(2),
First1 INT,
Second1 INT
)
INSERT INTO #tempTable (Place,First1,Second1)
values ('A1',1,8),('A2',2,5),('B1',4,6),('B2',3,7)
SELECT * from #tempTable
From this:
Place First1 Second1
A1 1 8
A2 2 5
B1 4 6
B2 3 7
To this:
A1 A2 B1 B2
First1 1 2 4 3
Second1 8 5 6 7
答案 1 :(得分:0)
只需在此处添加toString()
viewHolder.setImage(getApplicationContext(), model.getImage().toString());
或者您可以导入毕加索并添加此
compile 'com.squareup.picasso:picasso:2.5.2'
然后
@Override
protected void populateViewHolder(MyViewHoler viewHolder, GridItem model, int position) {
final String imageData = getRef(position).getKey();
// viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
Picasso.with(viewHolder.imageView.getContext()).load(model.getUrl()).into(viewHolder.imageView); //where imageView is where you want to put your firebase image
}
};