我想显示卡片视图,但它没有显示它显示致命异常,任何人都可以使用您的编程技巧解决此错误。请查看所有布局和类?
public class MainActivity extends AppCompatActivity {
List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView)findViewById(R.id.recycle);
rv.setHasFixedSize(true);
initializeData();
LinearLayoutManager ll = new LinearLayoutManager(this);
rv.setLayoutManager(ll);
RvAdapter rvAdapter = new RvAdapter(persons);
rv.setAdapter(rvAdapter);
}
private void initializeData() {
persons = new ArrayList<>();
persons.add(new Person("Anilkumar","23 years",R.drawable.anil));
persons.add(new Person("Prashanthi","21 years",R.drawable.inr));
}
}
人类
public class Person
{
String name;
String age;
int photoId;
Person(String name,String age,int photoId)
{
this.name = name;
this.age = age;
this.photoId = photoId;
}
}
RVadapter类
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.PersonViewHolder>
{
private List<Person> persons;
RvAdapter(List<Person> persons)
{
this.persons = persons;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout,parent,false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.personName.setText(persons.get(position).name);
holder.personName.setText(persons.get(position).age);
holder.personPhoto.setImageResource(persons.get(position).photoId);
}
@Override
public int getItemCount() {
return persons.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class PersonViewHolder extends RecyclerView.ViewHolder
{
CardView cardView;
TextView personName;
TextView personAge;
ImageView personPhoto;
public PersonViewHolder(View itemView) {
super(itemView);
cardView = (CardView)itemView.findViewById(R.id.card);
personName = (TextView)itemView.findViewById(R.id.text);
personAge = (TextView)itemView.findViewById(R.id.text2);
personPhoto = (ImageView)itemView.findViewById(R.id.image);
}
}
}
Logcat错误
06-20 22:51:57.126 5433-5433/com.example.anilkumar.recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
at android.content.res.Resources.loadDrawable(Resources.java:1940)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:368)
at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:177)
at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:170)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:53)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:74)
at com.example.anilkumar.recyclerview.RvAdapter.onBindViewHolder(RvAdapter.java:49)
at com.example.anilkumar.recyclerview.RvAdapter.onBindViewHolder(RvAdapter.java:16)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5746)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5779)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5016)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4892)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3239)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2767)
at android.view.View.measure(View.java:12723)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
答案 0 :(得分:0)
你提到它的大图像,处理太大的图像时必须注意内存问题。 您可以使用Picasso或Universal Image Loader或Glide
Theese库将异步加载您的图像,并且您不会遇到任何内存问题。
到目前为止,我最大的图像尺寸之一就像(4560x5250)
而我没有遇到任何内存问题,但如果你想要平滑滚动,你可以减少Picasso
中的图像尺寸。
目前我在Picasso
中使用RecyclerView
。我可以举例说明Picasso
:
在onBindViewHolder
:
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.personName.setText(persons.get(position).name);
holder.personAge.setText(persons.get(position).age);
Picasso.with(context).load(persons.get(position).photoId).into(holder.personPhoto);
}
您还可以找到有关theese图书馆的比较:picasso-v-s-imageloader-v-s-fresco-vs-glide