gpt4 book ai didi

java - 堆栈溢出错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:49 25 4
gpt4 key购买 nike

我正在使用Retrofit、picasso和recyclerview。但是出现了以下错误。

Process: com.example.user.recyclerview, PID: 4871 java.lang.StackOverflowError

MainActivity.class

    public class MainActivity extends AppCompatActivity
{
private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<ModelData> modelDataList;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = (RecyclerView) findViewById(R.id.card_recycle_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
getPopularMovies();
}

private void getPopularMovies()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://pratikbutani.x10.mx")
.addConverterFactory(GsonConverterFactory.create())
.build();

RetrofitInterface service = retrofit.create(RetrofitInterface.class);
Call<ModelDataList> call =service.getJSON();

call.enqueue(new Callback<ModelDataList>()
{
@Override
public void onResponse(Call<ModelDataList> call, Response<ModelDataList> response)
{
ModelDataList modelDataList = response.body();
dataAdapter = new DataAdapter(modelDataList.getResults());
recyclerView.setAdapter(dataAdapter);
}

@Override
public void onFailure(Call<ModelDataList> call, Throwable t)
{
Log.d("Main","failure");
}
});
}
}

ModelDataList.java

 public class ModelDataList
{
private List<ModelData> results;

public List<ModelData> getResults()
{
return results;
}
}

DataAdapter.java

      public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder>
{
private List<ModelData> modelDataList;
private LayoutInflater layoutInflater;
private Context context;

public DataAdapter(List<ModelData> modelDataList)
{
this.context = new AppCompatActivity();
this.modelDataList = modelDataList;
this.layoutInflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = layoutInflater.inflate(R.layout.card_view, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
ModelData modelData = modelDataList.get(position);
holder.textView.setText(modelData.getName());
Picasso.with(context).load(String.valueOf(modelData.getDisplay_pic())).placeholder(R.color.colorAccent).into(holder.imgview);
}

@Override
public int getItemCount()
{
return modelDataList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder
{
private ImageView imgview;
private TextView textView;

public ViewHolder(View itemView)
{
super(itemView);
imgview = (ImageView) itemView.findViewById(R.id.tv_image);
textView = (TextView) itemView.findViewById(R.id.tv_name);
}
}
}

最佳答案

不确定您的 StackOverflowError 问题。一个问题是你如何获得……背景。您可以从 View 中获取上下文,而不是“this.context = new AppCompatActivity();”前任: LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false); Picasso.with(holder.imgview.getContext()).load(String.valueOf(modelData.getDisplay_pic())).placeholder(R.color.colorAccent).into(holder.imgview);

关于java - 堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042552/

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