我使用FirebaseDatabase创建在线测验应用程序。当我在模拟器中启动应用程序(Genymotion:Samsung Galaxy S5 4.4.4)时,可以从我的类别模型中加载图像,但是当我在手机(华为P7-L10 4.4.2和其他手机)中启动应用程序时,我无法加载我的类别模型中的图片。在我的手机中未显示图像。
在我的Logcat中,当我在手机中启动应用程序时显示此内容
“”“ W / ClassMapper:在类Model.Category上找不到图像的设置器/字段”“
//我的Category.class模型
public class Category {
private String Name;
private String Image;
public Category(){
}
public Category(String name, String image){
this.Name = name;
this.Image = image;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
this.Image = image;
}}
//我的CategoryFragment.class
public class CategoryFragment extends Fragment {
View myFragment;
RecyclerView listCategory;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category,CategoryViewHolder> adapter;
FirebaseDatabase database;
DatabaseReference categories;
public static CategoryFragment newInstance(){
CategoryFragment categoryFragment = new CategoryFragment();
return categoryFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = FirebaseDatabase.getInstance();
categories = database.getReference("Category");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
myFragment =
inflater.inflate(R.layout.fragment_category,container,false);
listCategory = myFragment.findViewById(R.id.listCategory);
listCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(container.getContext());
listCategory.setLayoutManager(layoutManager);
loadCategories();
return myFragment;
}
public void loadCategories(){
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories
) {
@Override
protected void populateViewHolder(CategoryViewHolder viewHolder, f
final Category model, int position) {
viewHolder.category_name.setText(model.getName());
Picasso.with(getActivity())
.load(model.getImage())
.into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(getActivity(),String.format("%s|%s",adapter.getRef(position).getKey(),model.getName()),Toast.LENGTH_SHORT).show();
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}}
// CategoryViewHolder.class
public class CategoryViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView category_name;
public ImageView category_image;
private ItemClickListener itemClickListener;
public CategoryViewHolder(@NonNull View itemView) {
super(itemView);
category_image = itemView.findViewById(R.id.category_image);
category_name = itemView.findViewById(R.id.category_name);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}}
//我的ItemClickListener
public interface ItemClickListener {
void onClick(View view, int position, boolean isLongClick);}
//我的category_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Category Name"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center"
android:background="#4f0e0d0e"/>
//我的fragment_category.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".CategoryFragment"
android:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:id="@+id/listCategory"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>