gpt4 book ai didi

java - 错误 : incompatible types: HomeFragment cannot be converted to Context

转载 作者:搜寻专家 更新时间:2023-11-01 08:19:03 34 4
gpt4 key购买 nike

我的 Adapter 出错,这是我的代码,错误指向这段代码,它在 HomeFragment.java 上,它说:

error: incompatible types: HomeFragment cannot be converted to Context

adapter = new Adapter(models,this);

这是我的HomeFragment.java

public class HomeFragment extends Fragment {

//cardview
ViewPager viewPager;
Adapter adapter;
List<Model> models;
Integer[]colors = null;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);

//cardview
models = new ArrayList<>();
models.add(new Model(R.drawable.brochure,"Brochure","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.sticker,"Sticker","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.poster,"Poster","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.namecard,"Name Card","Lorem ipsum dolor sit amet"));

adapter = new Adapter(models,this);

viewPager = getView().findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
}
}

然后这是我的Adapter.java

public class Adapter extends PagerAdapter {

private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;

public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}

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

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

@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.item,container,false);

ImageView imageView;
TextView title,desc;

imageView = view.findViewById(R.id.image);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.desc);

imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDesc());

container.addView(view,0);
return view;
}

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

最佳答案

改变这个:

adapter = new Adapter(models,this);

为此:

adapter = new Adapter(models, (Context) getActivity());

您必须传递 ContextActivity 可以转换为 ContextFragment 则不行。

因此,您必须检索当前 Activity 的实例,这是我们通过 getActivity() 完成的。

如果你想在 Activity 中创建 Adapter 的实例,那么你可以通过简单地传递 this 来实现,就像我之前说的, Activity 可以强制转换为 ContextFragment 则不行。所以 Activity 将被转换为 Context,但是当你想在 Fragment 中创建相同的内容时,你不能简单地传递 this,因此,它引发了一个错误。

正如您在评论中所说,现在还有另一个错误,那是因为您已经在该方法的第一行returned 了一些东西,因此,永远不会到达该方法下面的每一行,这就是为什么会出现该错误,请放置此行

return inflater.inflate(R.layout.fragment_home,container,false);

最后,错误将消失。

在您的方法中进行更多编辑后,您的 OnCreateView 将如下所示:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_home, container, false); // Created a instance of View which we can return later.

//cardview
models = new ArrayList<>();
models.add(new Model(R.drawable.brochure,"Brochure","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.sticker,"Sticker","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.poster,"Poster","Lorem ipsum dolor sit amet"));
models.add(new Model(R.drawable.namecard,"Name Card","Lorem ipsum dolor sit amet"));

adapter = new Adapter(models,this);

viewPager = view.findViewById(R.id.viewPager); // Change getView() to view here.
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);

return view; // Returned the view.
}

关于java - 错误 : incompatible types: HomeFragment cannot be converted to Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54002865/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com