Stackoverflow错误

时间:2017-05-18 08:32:33

标签: java android

我正在使用Retrofit,picasso和recyclerview.But以下错误即将来临。

  

处理: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);
    }
}
   }

1 个答案:

答案 0 :(得分:0)

不确定您的StackOverflowError问题。一个问题是你如何得到......背景。您可以从视图中获取上下文而不是“this.context = new AppCompatActivity();” 例如:  LayoutInflater.from(parent.getContext())。inflate(R.layout.card_view,parent,false);     。Picasso.with(holder.imgview.getContext())的负载(将String.valueOf(modelData.getDisplay_pic()))占位符(R.color.colorAccent).into(holder.imgview);