当我向下滚动到列表底部时会抛出错误但如果不滚动一切正常,请帮助我。这是我的代码:
customadapter.java
public class CustomAdapter extends ArrayAdapter<String> {
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public CustomAdapter(Context context, String[] players, int[] images) {
super(context, R.layout.rowmodel, players);
this.c = context;
this.players = players;
this.images = images;
}
public class ViewHolder {
TextView nameTv;
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rowmodel, null);
}
ViewHolder holder = new ViewHolder();
holder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
holder.img = (ImageView) convertView.findViewById(R.id.imageView);
holder.nameTv.setText(players[position]);
holder.img.setImageResource(images[position]);
return convertView;
}
MainActivity.java
public class MainActivity extends ListActivity {
String[] players={" Ronaldo"," Modric"," Marcelo","Amanda","ola","andrea","girl"};
int[]images={R.drawable.ronaldo,R.drawable.modric,R.drawable.marcelo,R.drawable.amanda,R.drawable.ola,R.drawable.andrea,R.drawable.girl};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomAdapter adapter=new CustomAdapter(this,players,images);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this,l.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFF0EEEE"
tools:context="com.example.qais.lastchance.MainActivity">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp">
</ListView>
</RelativeLayout>
rowmodel.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/listshape">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cristiano Ronaldo"
android:id="@+id/nameTv"
android:layout_gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView"
android:layout_gravity="left"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这是错误
04-25 18:07:10.911 1781-1781/com.example.qais.lastchance E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.qais.lastchance, PID: 1781 java.lang.OutOfMemoryError: Failed to allocate a 423360012 byte allocation with 11587572 free bytes and 111MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037) at android.content.res.Resources.loadDrawableForCookie(Resources.java:4037) at android.content.res.Resources.loadDrawable(Resources.java:3910) at android.content.res.Resources.getDrawable(Resources.java:1982) at android.content.Context.getDrawable(Context.java:409) at android.widget.ImageView.resolveUri(ImageView.java:758) at android.widget.ImageView.setImageResource(ImageView.java:409) at com.example.qais.lastchance.CustomAdapter.getView(CustomAdapter.java:52) at android.widget.AbsListView.obtainView(AbsListView.java:2826) at android.widget.ListView.makeAndAddView(ListView.java:1932) at android.widget.ListView.fillUp(ListView.java:751) at android.widget.ListView.layoutChildren(ListView.java:1727) at android.widget.AbsListView.onLayout(AbsListView.java:2624) at android.view.View.layout(View.java:16992) at android.view.ViewGroup.layout(ViewGroup.java:5409) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16992) at android.view.ViewGroup.layout(ViewGroup.java:5409) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16992) at android.view.ViewGroup.layout(ViewGroup.java:5409) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16992) at android.view.ViewGroup.layout(ViewGroup.java:5409) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16992) at android.view.ViewGroup.layout(ViewGroup.java:5409) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2483) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2185) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1314) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7062) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:829) at android.view.Choreographer.doCallbacks(Choreographer.java:606) at android.view.Choreographer.doFrame(Choreographer.java:576) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:815) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6917) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:0)
这是一个内存不足的例外,您的图片(您尝试加载到图片视图中的可绘制内容)太大。
答案 1 :(得分:0)
作为建议recycleView,由于视图持有者模式,这将使您使用较少的内存量。
并且对于您的图像使用void load_books(){
ifstream myfile(path, ios::in);
if (myfile.fail()){
cout << "coudln't open file" << "\n\n";
}
else{
while (myfile){
myfile >> book1[i].id >> book1[i].title >> book1[i].p_name >> book1[i].p_address >> book1[i].aut_name;
myfile >> book1[i].aut_nationality >> book1[i].date >> book1[i].status;
cout << book1[i].id << "\ " << book1[i].title << "\ " << book1[i].p_name << "\ " << book1[i].p_address << "\ " << book1[i].aut_name;
cout << "\ " << book1[i].aut_nationality << "\ " << book1[i].date << "\ " << book1[i].status << endl;
i++;
}
myfile.close();
}
}
库来使图像加载更平滑并处理缓存
答案 2 :(得分:0)
Try this:
public class CustomAdapter extends ArrayAdapter<String> {
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public CustomAdapter(Context context, String[] players, int[] images) {
super(context, R.layout.rowmodel, players);
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.c = context;
this.players = players;
this.images = images;
}
public class ViewHolder {
TextView nameTv;
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if(v == null){
v = inflater.inflate(R.layout.rowmodel, null);
// well set up the ViewHolder
holder = new ViewHolder();
holder.nameTv = (TextView) v.findViewById(R.id.nameTv);
holder.img = (ImageView) v.findViewById(R.id.imageView);
// store the holder with the view.
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
holder.nameTv.setText(players[position]);
holder.img.setImageResource(images[position]);
return v;
}