Motorola Xoom os3.0.1
在 homeView里面,快速滑动ListView,会出现:out of memory
异常信息如下:
11-15 16:50:55.710: DEBUG/dalvikvm(15350): GC_FOR_ALLOC freed 15K, 1% free 45420K/45703K, paused 28ms
11-15 16:50:55.760: INFO/dalvikvm-heap(15350): Clamp target GC heap from 48.192MB to 48.000MB
11-15 16:50:55.760: DEBUG/dalvikvm(15350): GC_FOR_ALLOC freed <1K, 1% free 47202K/47495K, paused 28ms
11-15 16:50:55.900: INFO/dalvikvm-heap(15350): Clamp target GC heap from 49.680MB to 48.000MB
11-15 16:50:55.900: DEBUG/dalvikvm(15350): GC_FOR_ALLOC freed 24K, 1% free 48726K/49031K, paused 30ms
11-15 16:50:55.900: INFO/dalvikvm-heap(15350): Forcing collection of SoftReferences for 240016-byte allocation
11-15 16:50:55.940: INFO/dalvikvm-heap(15350): Clamp target GC heap from 49.679MB to 48.000MB
11-15 16:50:55.940: DEBUG/dalvikvm(15350): GC_BEFORE_OOM freed 11K, 1% free 48715K/49031K, paused 38ms
11-15 16:50:55.940: ERROR/dalvikvm-heap(15350): Out of memory on a 240016-byte allocation.
11-15 16:50:55.940: INFO/dalvikvm(15350): "main" prio=5 tid=1 RUNNABLE
11-15 16:50:55.940: INFO/dalvikvm(15350): | group="main" sCount=0 dsCount=0 obj=0x401a9680 self=0x115d8
11-15 16:50:55.940: INFO/dalvikvm(15350): | sysTid=15350 nice=0 sched=0/0 cgrp=default handle=-1342909328
11-15 16:50:55.940: INFO/dalvikvm(15350): | schedstat=( 12147025000 1019800000 5640 ) utm=1046 stm=168 core=1
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.graphics.Bitmap.nativeCreate(Native Method)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.graphics.Bitmap.createBitmap(Bitmap.java:531)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.View.buildDrawingCache(View.java:8703)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.View.getDrawingCache(View.java:8557)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.drawChild(ViewGroup.java:2382)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.widget.AbsListView.dispatchDraw(AbsListView.java:2023)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.widget.ListView.dispatchDraw(ListView.java:3220)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.View.draw(View.java:9135)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.widget.AbsListView.draw(AbsListView.java:3264)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.drawChild(ViewGroup.java:2508)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.View.draw(View.java:9032)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.drawChild(ViewGroup.java:2508)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.View.draw(View.java:9032)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.widget.FrameLayout.draw(FrameLayout.java:419)
11-15 16:50:55.950: INFO/dalvikvm(15350): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1910)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewRoot.draw(ViewRoot.java:1608)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewRoot.performTraversals(ViewRoot.java:1329)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.view.ViewRoot.handleMessage(ViewRoot.java:1944)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.os.Looper.loop(Looper.java:126)
11-15 16:50:55.950: INFO/dalvikvm(15350): at android.app.ActivityThread.main(ActivityThread.java:3997)
11-15 16:50:55.950: INFO/dalvikvm(15350): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 16:50:55.960: INFO/dalvikvm(15350): at java.lang.reflect.Method.invoke(Method.java:491)
11-15 16:50:55.960: INFO/dalvikvm(15350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-15 16:50:55.960: INFO/dalvikvm(15350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-15 16:50:55.960: INFO/dalvikvm(15350): at dalvik.system.NativeStart.main(Native Method)
11-15 16:50:55.990: INFO/dalvikvm-heap(15350): Clamp target GC heap from 49.678MB to 48.000MB
--------------------------------
发现问题可能出在: HomeActivity.java当中的adapter
/**
* This is a class that used to as a adapter for ListView.
*/
private static class GroupListAdapter extends ArrayAdapter<String>{
public GroupListAdapter(Context context, List<String> list) {
super(context, 0, list);
}
@Override
public boolean isEnabled(int position) {
//index didn't contain the ':'.
if(getItem(position).indexOf(":")==-1){
return false;
}
return super.isEnabled(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Distinguish the index and data item of the List and give them the different layout.
View view = convertView;
////index didn't contain the ':',and data contains.
if(getItem(position).indexOf(":")==-1){
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_index_item, null);
TextView dateView = (TextView) view.findViewById(R.id.problemIndexName);
dateView.setText(getItem(position));
}else{
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_list_item, null);
TextView idView = (TextView) view.findViewById(R.id.problemId);
idView.setText(getItem(position).substring(0,getItem(position).indexOf(":")));
TextView titleView = (TextView) view.findViewById(R.id.problemName);
titleView.setText(getItem(position).substring(getItem(position).indexOf(":")+1));
}
return view;
}
}
convertView没有重用!!!!不知道为什么,手机端没有这样的问题,而平板居然会有这样的问题
修改version1:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView != null){
System.out.println("convertView != null....convertView");
}
//Distinguish the index and data item of the List and give them the different layout.
View view = null;
if(convertView == null){
////index didn't contain the ':',and data contains.
if(getItem(position).indexOf(":")==-1){
//view = indexView;
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_index_item, null);
view.setTag("IndexView");
TextView dateView = (TextView) view.findViewById(R.id.problemIndexName);
dateView.setText(getItem(position));
}else{
//view = problemView;
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_list_item, null);
view.setTag("ProblemView");
TextView idView = (TextView) view.findViewById(R.id.problemId);
idView.setText(getItem(position).substring(0,getItem(position).indexOf(":")));
TextView titleView = (TextView) view.findViewById(R.id.problemName);
titleView.setText(getItem(position).substring(getItem(position).indexOf(":")+1));
}
}else{ // convertView != null
view = convertView;
// to do......
Object tag = view.getTag();
if(tag != null){
if("IndexView".equals(tag.toString())){
if(getItem(position).indexOf(":")==-1){
TextView dateView = (TextView) view.findViewById(R.id.problemIndexName);
dateView.setText(getItem(position));
}else{
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_list_item, null);
view.setTag("ProblemView");
TextView idView = (TextView) view.findViewById(R.id.problemId);
idView.setText(getItem(position).substring(0,getItem(position).indexOf(":")));
TextView titleView = (TextView) view.findViewById(R.id.problemName);
titleView.setText(getItem(position).substring(getItem(position).indexOf(":")+1));
}
}else if("ProblemView".equals(tag.toString())){
if(getItem(position).indexOf(":")==-1){
view = LayoutInflater.from(getContext()).inflate(R.layout.problem_index_item, null);
view.setTag("IndexView");
TextView dateView = (TextView) view.findViewById(R.id.problemIndexName);
dateView.setText(getItem(position));
}else{
TextView idView = (TextView) view.findViewById(R.id.problemId);
idView.setText(getItem(position).substring(0,getItem(position).indexOf(":")));
TextView titleView = (TextView) view.findViewById(R.id.problemName);
titleView.setText(getItem(position).substring(getItem(position).indexOf(":")+1));
}
}
}
}
return view;
}
------------------------测试了一下,发现好多了,可是当你快速滑动的时候,还是有这样的问题。
搜索了一下发现(“ListView convertView重用,多种item布局):
http://www.eoeandroid.com/thread-72369-1-1.html
这个帖子巨好!!!
修改后的代码:
/**
* the layout of ListView's Item
*/
class ViewHolderIndexView{
TextView dateView;
}
class ViewHolderProblemView{
TextView idView,titleView;
}
/**
* This is a class that used to as a adapter for ListView.
*/
private class GroupListAdapter extends ArrayAdapter<String>{
LayoutInflater inflater;
final int TYPE_INDEX_VIEW = 0;
final int TYPE_PROBLEM_VIEW = 1;
public GroupListAdapter(Context context, List<String> list) {
super(context, 0, list);
inflater = LayoutInflater.from(context);
}
//every convertView will invoke this method, get the wanted view style
@Override
public int getItemViewType(int position) {
if(getItem(position).indexOf(":")==-1){
return TYPE_INDEX_VIEW;
}else{
return TYPE_PROBLEM_VIEW;
}
}
@Override
public int getViewTypeCount(){
return 2;
}
@Override
public boolean isEnabled(int position) {
//index didn't contain the ':'.
if(getItem(position).indexOf(":")==-1){
return false;
}
return super.isEnabled(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderIndexView holder_IndexView = null;
ViewHolderProblemView holder_problemView = null;
int type = getItemViewType(position);
if(convertView == null){
switch(type){
case TYPE_INDEX_VIEW:
//convertView = inflater.inflate(R.layout.problem_index_item, parent,false); 注意:用这样的方式布局无法保持原来的样子
convertView = inflater.inflate(R.layout.problem_index_item, null);
holder_IndexView = new ViewHolderIndexView();
holder_IndexView.dateView = (TextView)convertView.findViewById(R.id.problemIndexName);
convertView.setTag(holder_IndexView);
break;
case TYPE_PROBLEM_VIEW:
//convertView = inflater.inflate(R.layout.problem_list_item, parent,false);注意:用这样的方式布局无法保持原来的样子
convertView = inflater.inflate(R.layout.problem_list_item, null);
holder_problemView = new ViewHolderProblemView();
holder_problemView.idView = (TextView)convertView.findViewById(R.id.problemId);
holder_problemView.titleView = (TextView)convertView.findViewById(R.id.problemName);
convertView.setTag(holder_problemView);
break;
}
}else{ // convertView != null
switch(type){
case TYPE_INDEX_VIEW:
holder_IndexView = (ViewHolderIndexView)convertView.getTag();
break;
case TYPE_PROBLEM_VIEW:
holder_problemView = (ViewHolderProblemView)convertView.getTag();
break;
}
}
//Distinguish the index and data item of the List and give them the different layout.
// set data for layout
switch(type){
case TYPE_INDEX_VIEW:
holder_IndexView.dateView.setText(getItem(position));
break;
case TYPE_PROBLEM_VIEW:
holder_problemView.idView.setText(getItem(position).substring(0,getItem(position).indexOf(":")));
holder_problemView.titleView.setText(getItem(position).substring(getItem(position).indexOf(":")+1));
break;
}
return convertView;
}
}
问题解决了!!!!哈哈