我收到错误OutofMemory和更多例外 **不加载drawable文件夹中的所有数据..plz建议我任何解决方案..
Main.java
public class Main extends Activity{
ListView list;
String[] pool = { "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A" };
String[] date1 = {
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
"01-02-2014",
} ;
String[] venue = { "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia fgasfhdgsdj" , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia fgasfhdgsdj" , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia "};
String[] time = { "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm" };
String[] country1 = {
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia"
} ;
String[] country2 = {
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia",
"India",
"New Zealand",
"West Indies",
"West Indies",
"Australia",
"Australia",
"Australia"
} ;
String[] vs = { "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs" };
Integer[] imageId = {
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka
};
Integer[] imageId2 = {
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
R.drawable.afghanistan,
R.drawable.australia,
R.drawable.bangladesh,
R.drawable.england,
R.drawable.newzealand,
R.drawable.scotland,
R.drawable.srilanka,
};
@Override
protected void onCreate(Bundle savedInstanceState) throws OutOfMemoryError {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
try
{
Custom_List1 adapter = new Custom_List1(Main.this, pool, date1, venue, time, country1, country2, vs, imageId, imageId2);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
这是arrayadapter类,我该如何解决这个问题 Custom_List1.java
public class Custom_List1 extends ArrayAdapter<String>{
private final Activity context;
private final String[] pool;
private final String[] date1;
private final String[] venue;
private final String[] time;
private final String[] country1; //web
private final String[] country2;
private final String[] vs;
private final Integer[] imageId;
private final Integer[] imageId2;
public Custom_List1(Activity context, String[] pool, String[] date1, String[] venue, String[] time, String[] country1, String[] country2,String[] vs, Integer[] imageId, Integer[] imageId2)
{
super(context, R.layout.list_single1, country1);
this.context = context;
this.pool = pool;
this.date1 = date1;
this.venue = venue;
this.time = time;
this.country1 = country1;
this.country2 = country2;
this.vs = vs;
this.imageId = imageId;
this.imageId2 = imageId2;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single1, null, true);
TextView poolT = (TextView) rowView.findViewById(R.id.txtPool);
TextView date1T = (TextView) rowView.findViewById(R.id.txtDate);
TextView venueT = (TextView) rowView.findViewById(R.id.txtVenue1);
TextView timeT = (TextView) rowView.findViewById(R.id.txtTime);
TextView country1T = (TextView) rowView.findViewById(R.id.txtCountry1);
TextView country2T = (TextView) rowView.findViewById(R.id.txtCountry2);
TextView vsT = (TextView) rowView.findViewById(R.id.vs);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
ImageView imageView1 = (ImageView) rowView.findViewById(R.id.img2);
poolT.setText(pool[position]);
date1T.setText(date1[position]);
venueT.setText(venue[position]);
timeT.setText(time[position]);
country1T.setText(country1[position]);
country2T.setText(country2[position]);
vsT.setText(vs[position]);
imageView.setImageResource(imageId[position]);
imageView1.setImageResource(imageId[position]);
return rowView;
}
}
list.xml
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:dividerHeight="10.0sp">
</ListView>
sinlglelist.xml
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="20sp"/>
<ImageView
android:id="@+id/img2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10sp"/>
</TableRow>
答案 0 :(得分:0)
将您的自定义适配器更改为:
public class Custom_List1 extends ArrayAdapter<String>{
...
@Override
public View getView(final int position, View rowView, ViewGroup parent) {
ViewHolder holder;
if (rowView == null) {
rowView = LayoutInflater.from(context).inflate(
R.layout.list_single1, null);
holder = new ViewHolder();
holder.poolT = (TextView) rowView.findViewById(R.id.txtPool);
holder.date1T = (TextView) rowView.findViewById(R.id.txtDate);
holder.venueT = (TextView) rowView.findViewById(R.id.txtVenue1);
holder.timeT = (TextView) rowView.findViewById(R.id.txtTime);
holder.country1T = (TextView) rowView.findViewById(R.id.txtCountry1);
holder.country2T = (TextView) rowView.findViewById(R.id.txtCountry2);
holder.vsT = (TextView) rowView.findViewById(R.id.vs);
holder.imageView = (ImageView) rowView.findViewById(R.id.img);
holder.imageView1 = (ImageView) rowView.findViewById(R.id.img2);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.poolT.setText(pool[position]);
holder.date1T.setText(date1[position]);
holder.venueT.setText(venue[position]);
holder.timeT.setText(time[position]);
holder.country1T.setText(country1[position]);
holder.country2T.setText(country2[position]);
holder.vsT.setText(vs[position]);
holder.imageView.setImageResource(imageId[position]);
holder.imageView1.setImageResource(imageId[position]);
return rowView;
}
public static class ViewHolder {
TextView poolT;
TextView date1T;
TextView venueT;
TextView timeT;
TextView country1T;
TextView country2T;
TextView vsT;
ImageView imageView ;
ImageView imageView1;
}
}
答案 1 :(得分:0)
首先,生成列表视图时,您的代码效率不高。 this answer可以帮助您解决问题。
其次,您一次处理大量资源,并且可以避免在弱设备上内存不足以防止应用程序崩溃并关闭,只需添加try catch:
catch(OutOfMemoryError e){}
最后,很多图书馆都可以optimize your listview construction和lazy load imageviews
希望有所帮助