0

Everytime I test my app, it crashes after the splashscreen and it gives errors like:

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

and

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

My ImageAdapter looks like this:

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

    }

And here the method in the main 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;
            }

        }
    });

}

I hope somebody can help me!

Thanks in advance ;) Wouter

wouterdz
  • 131
  • 1
  • 14

2 Answers2

1

This is because, you are not using the Lazy loading of images properly.

The correct way would be:

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

EDIT: Now replace your getView() with this getView().

Even this is not the best way. You should create a Holder which will hold your views properly.

To best understand how to reuse the garbage collected views, please see this: http://android.amberfog.com/?p=296

As you are facing OOME, you may also consider my answer on same type of question. bitmap size exceeds Vm budget error android

Thank you.

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • I have edited my answer. Also consider using Holder as mentioned in my first link. Also handle NPE for button and imageView. – Shrikant Ballal Feb 14 '13 at 12:41
  • Okay thanks, but it now says I have to initialize my ImageView variable. I'll probably rewrite my Adapter with a holder today. – wouterdz Feb 14 '13 at 12:43
0

If you are using ics with version 4.1.2 or higher, your problem may be related to the problem states at this site.

It is explained there that the latest versions of android have a different kind of a garbage collector and it handles the big data in a different way. I had the same problem and solved by using smaller images. I whish this solves your issue.

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
  • That may very well be the problem - a bug in Android. 4.1 is littered with them. As for image sizes, my images are 160px120px, so that is actually quite small. BTW, what is "ICS" (as mentioned in the link)? – Johann Jul 08 '13 at 10:02
  • Well yes your images are small enough but give it a try by smaller ones because this is the world of the software. "ICS" is "Ice Cream Sandwitch" which is the version of android with number 4.0. Yes i have written it with "4.1.2" which is false. I must had written "android" instead of "ics". – Bahadir Tasdemir Jul 09 '13 at 20:08