如何修复不兼容的类型:无法将片段转换为上下文。

huangapple go评论99阅读模式
英文:

How to fix incompatible types: fragment cannot be converted to Context

问题

这是出现问题的那一行代码:

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error

要解决这个问题,你可以尝试将 this 更改为 requireContext(),如下所示:

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));

这样应该可以解决你的错误。

英文:

i am trying to view items from my database and i have written everything to retrieve that and display it in an activity page. but this one line of code gives an error that doesn't allow the app to run:

here is my detailed code:

  1. public class HomeFragment extends Fragment {
  2. private String name;
  3. private String location;
  4. private String country;
  5. private String address;
  6. public HomeFragment() {
  7. }
  8. public HomeFragment(String name, String location, String country, String address) {
  9. this.name = name;
  10. this.location = location;
  11. this.country = country;
  12. this.address = address;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getLocation() {
  21. return location;
  22. }
  23. public void setLocation(String location) {
  24. this.location = location;
  25. }
  26. public String getCountry() {
  27. return country;
  28. }
  29. public void setCountry(String country) {
  30. this.country = country;
  31. }
  32. public String getAddress() {
  33. return address;
  34. }
  35. public void setAddress(String address) {
  36. this.address = address;
  37. }
  38. private FirebaseFirestore firebaseFirestore;
  39. private RecyclerView FirestoreList;
  40. private FirestoreRecyclerAdapter adapter;
  41. @Override
  42. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  43. Bundle savedInstanceState) {
  44. firebaseFirestore = FirebaseFirestore.getInstance();
  45. FirestoreList = FirestoreList.findViewById(R.id.firestore_list);
  46. //query
  47. Query q = firebaseFirestore.collection("Shops");
  48. //recycle options
  49. FirestoreRecyclerOptions<ShopModel> options = new FirestoreRecyclerOptions.Builder<ShopModel>().setQuery(q, ShopModel.class).build();
  50. adapter = new FirestoreRecyclerAdapter<ShopModel, ShopViewHolder>(options) {
  51. @NonNull
  52. @Override
  53. public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  54. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home,parent,false);
  55. return new ShopViewHolder(view);
  56. }
  57. @Override
  58. protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull ShopModel model) {
  59. holder.list_name.setText(model.getName());
  60. holder.list_country.setText(model.getName());
  61. holder.list_location.setText(model.getName());
  62. holder.list_address.setText(model.getName());
  63. }
  64. };
  65. FirestoreList.setHasFixedSize(true);
  66. FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error
  67. FirestoreList.setAdapter(adapter);
  68. return inflater.inflate(R.layout.fragment_home, container, false);
  69. }
  70. private class ShopViewHolder extends RecyclerView.ViewHolder {
  71. private TextView list_name;
  72. private TextView list_country;
  73. private TextView list_location;
  74. private TextView list_address;
  75. public ShopViewHolder(@NonNull View itemView) {
  76. super(itemView);
  77. list_name = itemView.findViewById(R.id.list_name);
  78. list_country = itemView.findViewById(R.id.list_country);
  79. list_location = itemView.findViewById(R.id.list_location);
  80. list_address = itemView.findViewById(R.id.list_address);
  81. }
  82. }
  83. @Override
  84. public void onStart() {
  85. super.onStart();
  86. adapter.startListening();
  87. }
  88. @Override
  89. public void onStop() {
  90. super.onStop();
  91. adapter.stopListening();
  92. }
  93. }

this is the line where there is a problem at

FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error

what can i do to resolve this?

答案1

得分: 2

你正在将你的片段传递给 LinearLayoutManager,而不是 Context

所以你需要做的就是简单地替换这行代码

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(this));

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));
英文:

You are passing your fragment to LinearLayoutManager, not the Context

So what you need to do is simply replace this line

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(this));

To

  1. FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));

答案2

得分: 0

请创建一个名为FirestoreList的对象,并尝试将布局管理器设置为该对象:

  1. FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
  2. ...
  3. fsList.setLayoutManager(new LinearLayoutManager(this));
英文:

Can you please make an object for FirestoreList and try to set the LayoutManager to the object ?

  1. FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
  2. ...
  3. fsList.setLayoutManager(new LinearLayoutManager(this));

答案3

得分: 0

将这个语句更改为:

  1. View view = inflater.inflate(R.layout.fragment_home, container, false);
英文:

Change this statement :
return inflater.inflate(R.layout.fragment_home, container, false);

to:
View view = inflater.inflate(R.layout.fragment_home, container, false);

huangapple
  • 本文由 发表于 2020年8月2日 20:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63215770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定