Android: strange crash on grid scroll-Collection of common programming errors
I’m creating a grid by a standard scheme: GridView
is set with a custom BaseAdapter
-derivate:
class EventGridAdapter extends BaseAdapter {
private GregorianCalendar gregorianCalendar;
Context context;
GridView gridView;
public EventGridAdapter(Context context, GridView gridView) {
this.context = context;
this.gridView = gridView;
}
public int getCount() {
return 31;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
CalendarItem cell = null;
if (convertView == null) {
cell = new CalendarItem(context);
int viewHeight = gridView.getHeight();
int spacing = 1;
int height = viewHeight / 5 - spacing;
cell.setMinimumHeight(height);
} else {
cell = (CalendarItem) convertView;
}
return cell;
}
}
About this line:
cell = new CalendarItem(context)
By now CalendarItem
is pure View
, e.g. everything is commented out, so nothing to cause crash there…
And standard GridView
output:
public class CalendarActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
GridView g = (GridView) findViewById(R.id.gridCalendar);
g.setAdapter(new EventGridAdapter(this, g));
}
}
Layout:
What is the problem:
When I run this code in emulator on ICS 4.0.3, grid is shown perfectly, but when I try to scroll it – program crashes with strange exception:
D/AndroidRuntime( 1087): Shutting down VM W/dalvikvm( 1087): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) E/AndroidRuntime( 1087): FATAL EXCEPTION: main E/AndroidRuntime( 1087): java.lang.IllegalArgumentException: bitmap size exceeds 32bits E/AndroidRuntime( 1087): at android.graphics.Bitmap.nativeCreate(Native Method) E/AndroidRuntime( 1087): at android.graphics.Bitmap.createBitmap(Bitmap.java:605) E/AndroidRuntime( 1087): at android.graphics.Bitmap.createBitmap(Bitmap.java:585) E/AndroidRuntime( 1087): at android.view.View.buildDrawingCache(View.java:10626) E/AndroidRuntime( 1087): at android.view.View.getDrawingCache(View.java:10476) E/AndroidRuntime( 1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2743) E/AndroidRuntime( 1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime( 1087): at android.widget.AbsListView.dispatchDraw(AbsListView.java:2092) E/AndroidRuntime( 1087): at android.view.View.draw(View.java:11083) E/AndroidRuntime( 1087): at android.widget.AbsListView.draw(AbsListView.java:3398) E/AndroidRuntime( 1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2887) E/AndroidRuntime( 1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime( 1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) E/AndroidRuntime( 1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime( 1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) E/AndroidRuntime( 1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime( 1087): at android.view.View.draw(View.java:10981) E/AndroidRuntime( 1087): at android.widget.FrameLayout.draw(FrameLayout.java:450) E/AndroidRuntime( 1087): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126) E/AndroidRuntime( 1087): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026) E/AndroidRuntime( 1087): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634) E/AndroidRuntime( 1087): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) E/AndroidRuntime( 1087): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 1087): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime( 1087): at android.app.ActivityThread.main(ActivityThread.java:4424) E/AndroidRuntime( 1087): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 1087): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime( 1087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) E/AndroidRuntime( 1087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) E/AndroidRuntime( 1087): at dalvik.system.NativeStart.main(Native Method)
When I run the same code in emulator on 2.3.3 – everything is fine!
After some investigation I found the “source” of the problem (-. Look again at getView
method in EventGridAdapter
:
int viewHeight = gridView.getHeight();
int spacing = 1;
int height = viewHeight / 5 - spacing;
cell.setMinimumHeight(height);
The problem line is:
cell.setMinimumHeight(height);
When I change it to:
cell.setMinimumHeight(10);
… crashes dissapear
But I don’t know what do I do wrong? Everything looks logically correct: I need 5 rows. Also, making height
lower doesn’t help (when all rows are in a view area, e.g. no scroll needed):
int height = viewHeight / 7 - spacing;
Your advices?
-
the problem is hear…
gridView.getHeight()
why? because you will not have the height of the gridview @ the time it just created
you can use this method in either
onWindowfocuschanged
or by using viewTreeobserverlike the one below
ViewTreeObserver vto1 = gridView.getViewTreeObserver(); vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { // write hear for height Log.i(TAG, "from VTO" + gridView.getHeight()); ViewTreeObserver obs = gridView.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });