每次測試我的應用程序,它在啓動畫面後崩潰,並讓這樣的錯誤:應用程序崩潰,因爲ImageAdapter /的GridView的
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;
}
}
而這裏的主要活動方法:
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;
}
}
});
}
我希望有人能幫助我!
在此先感謝;) 沃特
該代碼應該是我的整個ImageAdapter嗎? – wouterdz 2013-02-14 12:36:40
我編輯了我的答案。也請考慮使用我的第一個鏈接中提到的Holder。還要處理按鈕和imageView的NPE。 – Shrikant 2013-02-14 12:41:11
好的,謝謝,但它現在說我必須初始化我的ImageView變量。今天我可能會用持有人重寫我的適配器。 – wouterdz 2013-02-14 12:43:45