gpt4 book ai didi

java - 由于 ImageAdapter/Gridview 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 14:50:50 28 4
gpt4 key购买 nike

每次我测试我的应用程序时,它都会在闪屏后崩溃,并给出如下错误:

02-14 13:03:29.305: A/libc(9935): Fatal signal 11 (SIGSEGV) at 0x0000015c (code=1), thread        9935 (ter.planyourday)

02-14 13:03:29.260: E/dalvikvm-heap(9935): Out of memory on a 2381200-byte allocation.

我的 ImageAdapter 看起来像这样:

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] buttonValues;

public ImageAdapter(Context context, String[] buttonValues) {
this.context = context;
this.buttonValues = buttonValues;
}

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

if (convertView == null) {

gridView = new View(context);

// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);

// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);

String button = buttonValues[position];

if (button.equals("homework")) {
imageView.setImageResource(R.drawable.button_homework);
} else if (button.equals("schedule")) {
imageView.setImageResource(R.drawable.button_schedule);
} else if(button.equals("planner")) {
imageView.setImageResource(R.drawable.button_planner);
}else{
imageView.setImageResource(R.drawable.button_settings);
}

} else {
gridView = (View) convertView;
}

return gridView;
}

@Override
public int getCount() {
return buttonValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

}

这是主 Activity 中的方法:

static final String[] MOBILE_OS = new String[] { "homework", "schedule",
"planner", "settings" };
private void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);

gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
Intent startHomework = new Intent(HomeScreen.this,
HomeworkScreen.class);
HomeScreen.this.startActivity(startHomework);
break;
case 1:
Intent startSchedule = new Intent(HomeScreen.this,
ScheduleScreen.class);
HomeScreen.this.startActivity(startSchedule);
break;

case 2:
Intent startPlanner = new Intent(HomeScreen.this,
PlannerScreen.class);
HomeScreen.this.startActivity(startPlanner);

break;

case 3:
Intent startSettings = new Intent(HomeScreen.this,
SettingScreen.class);
HomeScreen.this.startActivity(startSettings);
break;
}

}
});

}

希望有人能帮助我!

提前致谢;)沃特

最佳答案

这是因为,您没有正确使用图像延迟加载。

正确的方法是:

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

ImageView imageView;

String button;

if (convertView == null) {

gridView = new View(context);

// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);

// set image based on selected text
imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
} else {
gridView = (View) convertView;
}
button = buttonValues[position];

if (button.equals("homework")) {
imageView.setImageResource(R.drawable.button_homework);
} else if (button.equals("schedule")) {
imageView.setImageResource(R.drawable.button_schedule);
} else if(button.equals("planner")) {
imageView.setImageResource(R.drawable.button_planner);
}else{
imageView.setImageResource(R.drawable.button_settings);
}

return gridView;
}

编辑:现在用这个 getView() 替换你的 getView() 。

即使这也不是最好的方法。您应该创建一个能正确保存您的观点的 Holder。

为了更好地了解如何重用垃圾收集的 View ,请参阅:http://android.amberfog.com/?p=296

当你面临OOME时,你也可以考虑我对同类问题的回答。 bitmap size exceeds Vm budget error android

谢谢。

关于java - 由于 ImageAdapter/Gridview 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874519/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com