简体   繁体   English

错误:不兼容的类型:上下文无法转换为 ArrayList<datamodel></datamodel>

[英]error: incompatible types: Context cannot be converted to ArrayList<dataModel>

I don't know what am I supposed to do, can somebody help me??我不知道我该怎么办,有人可以帮助我吗?

error: incompatible types: Context cannot be converted to ArrayList adapter = new myAdapter(getApplicationContext());错误:不兼容的类型:上下文无法转换为 ArrayList 适配器 = new myAdapter(getApplicationContext());

can you help me to solve my problem??你能帮我解决我的问题吗?

this is my MainActivity.java:这是我的 MainActivity.java:

adapter = new myAdapter(getApplicationContext());
recycler.setAdapter(adapter);

here is myAdapter.java:这是 myAdapter.java:

public class myAdapter extends RecyclerView.Adapter<myAdapter.myHolder> {
    ArrayList<dataModel> dataHolder;
    Context context;

    public myAdapter(ArrayList<dataModel> dataHolder) {
        this.dataHolder = dataHolder;
        //this.context = context;
    }
}

So, your getApplicationContext() method returns Context , but in your constructor you expect parameter of type ArrayList<dataModel> , that's why it throws exception.因此,您的getApplicationContext()方法返回Context ,但在您的构造函数中,您期望ArrayList<dataModel>类型的参数,这就是它抛出异常的原因。 Now you have 2 options:现在您有 2 个选项:

  • keep your constructor, but pass different parameter保留您的构造函数,但传递不同的参数

    adapter = new myAdapter(someArrayList);
  • change constructor, so that it accepts Context更改构造函数,使其接受Context

     public myAdapter(Context context) { this.dataHolder = new ArrayList<>(); this.context = context; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:不兼容的类型:SnekEngine 无法转换为 Context - error: incompatible types: SnekEngine cannot be converted to Context 错误:不兼容的类型:上下文无法转换为 HomeMenu - error: incompatible types: Context cannot be converted to HomeMenu 错误:类型不兼容:NewSubscription无法转换为上下文 - error: incompatible types: NewSubscription cannot be converted to Context 错误:不兼容的类型:ImageActivity 无法转换为 Context - error: incompatible types: ImageActivity cannot be converted to Context 错误:类型不兼容:HomeFragment无法转换为上下文 - error: incompatible types: HomeFragment cannot be converted to Context 错误:(31、32)错误:类型不兼容:FragmentOne无法转换为上下文 - Error:(31, 32) error: incompatible types: FragmentOne cannot be converted to Context 错误:(124,62)错误:不兼容的类型:类无法转换为上下文 - Error:(124, 62) error: incompatible types: Class cannot be converted to Context 错误:(23,39)错误:类型不兼容:视图无法转换为上下文 - Error:(23, 39) error: incompatible types: View cannot be converted to Context 不兼容的类型:Widget 无法转换为 Context - Incompatible types: Widget cannot be converted to Context 不兼容的类型: <anonymous TextWatcher> 无法转换为上下文 - incompatible types: <anonymous TextWatcher> cannot be converted to Context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM