由于ImageAdapter / Gridview,应用程序崩溃

时间:2013-02-14 12:08:04

标签: java android gridview crash

每当我测试我的应用时,它会在启动画面后崩溃,并且会出现如下错误:

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;
            }

        }
    });

}

我希望有人可以帮助我!

提前致谢;) 沃特

2 个答案:

答案 0 :(得分:1)

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

正确的方法是:

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()。

即使这不是最好的方法。你应该创建一个能够正确保存你的观点的持有人。

要最好地了解如何重复使用垃圾回收的视图,请参阅:http://android.amberfog.com/?p=296

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

谢谢。

答案 1 :(得分:0)

如果您使用版本4.1.2或更高版本的ics,则问题可能与此site上的问题状态有关。

在那里解释说,最新版本的android有一个不同类型的垃圾收集器,它以不同的方式处理大数据。我有同样的问题,并通过使用较小的图像解决。我问这解决了你的问题。