首页 > 解决方案 > com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型

问题描述

com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型

com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型

包 com.areef.pamur.EndUser;公共类 ClothingActivity 扩展 AppCompatActivity {

private DatabaseReference ClothingRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private String type = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_clothing);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing");

    recyclerView = findViewById(R.id.clothing_category_list);
    recyclerView.setHasFixedSize(true);
    layoutManager = new GridLayoutManager(this, 3);
    recyclerView.setLayoutManager(layoutManager);


}

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerOptions<ClothingCategory> options =
            new FirebaseRecyclerOptions.Builder<ClothingCategory>()
                    .setQuery(ClothingRef.orderByChild("name"), ClothingCategory.class).build();


    FirebaseRecyclerAdapter<ClothingCategory, ClothingCategoryViewHolder> adapter =
            new FirebaseRecyclerAdapter<ClothingCategory, ClothingCategoryViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull ClothingCategoryViewHolder holder, int position, @NonNull final ClothingCategory model) {


                    holder.txtClothingCategorytName.setText(model.getName());
                    Picasso.get().load(model.getImage()).into(holder.clothingCategoryImageView);


                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (type.equals("Admin")) {
                                Intent intent = new Intent(ClothingActivity.this, AdminMaintainProductsActivity.class);
                                intent.putExtra("pid", model.getPid());
                                startActivity(intent);
                            } else if (model.getName().equals("Men")) {
                                Intent intent = new Intent(ClothingActivity.this, ClothingActivity.class);
                                intent.putExtra("name", model.getName());
                                startActivity(intent);
                            } else if (model.getName().equals("Women")) {
                                Intent intent = new Intent(ClothingActivity.this, ElectronicsActivity.class);
                                intent.putExtra("name", model.getName());
                                startActivity(intent);
                            } else if (model.getName().equals("Kids")) {
                                Intent intent = new Intent(ClothingActivity.this, FoodActivity.class);
                                intent.putExtra("name", model.getName());
                                startActivity(intent);
                            }


                        }
                    });
                }

                @NonNull
                @Override
                public ClothingCategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_clothing_items_layout, parent, false);
                    ClothingCategoryViewHolder holder = new ClothingCategoryViewHolder(view);
                    return holder;
                }
            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();


}   

}

  1. 查看持有人类别
  2. 模型类
  3. 火力基地

标签: javaandroidfirebasefirebase-realtime-database

解决方案


因为您没有提供导致此异常的操作,所以我只能参考 Firebase 的文档来找出您的异常。

https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseException

但据我所见,您的代码存在问题。

  1. 该变量type在声明后保留为空字符串,因此您放置在onBindViewHolder()反对中的条件语句type不会生成输出Admin

  2. 该问题很可能是由您的 Firebase 结构和您尝试检索数据的方式引起的。Clothing您在as Kids&Men和数据库参考中有孩子-

    ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing"); 您没有访问Clothing. 因此,您正在访问一个无效的数据集。

我的建议是为你的查询语句创建一个模型类Kids并修改你的查询语句 ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing").child("Kids);,看看你的输出是什么。

有关任何其他信息,请参阅实时数据库的官方 FirebaseUI 文档 - https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/


推荐阅读