{"id":1913,"date":"2022-08-30T15:20:29","date_gmt":"2022-08-30T15:20:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/android-strange-crash-on-grid-scroll-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:29","modified_gmt":"2022-08-30T15:20:29","slug":"android-strange-crash-on-grid-scroll-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-strange-crash-on-grid-scroll-collection-of-common-programming-errors\/","title":{"rendered":"Android: strange crash on grid scroll-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m creating a grid by a standard scheme: <code>GridView<\/code> is set with a custom <code>BaseAdapter<\/code>-derivate:<\/p>\n<pre><code>class EventGridAdapter extends BaseAdapter {\n\n    private GregorianCalendar gregorianCalendar;\n    Context context;\n    GridView gridView;\n\n    public EventGridAdapter(Context context, GridView gridView) {\n        this.context = context;\n        this.gridView = gridView;\n    }\n\n\n    public int getCount() {\n        return 31;\n    }\n\n    public Object getItem(int position) {\n        return null;\n    }\n\n    public long getItemId(int position) {\n        return position;\n    }\n\n    public View getView(int position, View convertView, ViewGroup parent) {\n\n        CalendarItem cell = null;\n        if (convertView == null) {\n            cell = new CalendarItem(context);\n            int viewHeight = gridView.getHeight();\n            int spacing = 1;\n            int height = viewHeight \/ 5 - spacing; \n            cell.setMinimumHeight(height);\n        } else {\n            cell = (CalendarItem) convertView;\n        }\n        return cell;\n    }\n} \n<\/code><\/pre>\n<p>About this line:<\/p>\n<pre><code>cell = new CalendarItem(context)\n<\/code><\/pre>\n<p>By now <code>CalendarItem<\/code> is pure <code>View<\/code>, e.g. everything is commented out, so nothing to cause crash there&#8230;<\/p>\n<p>And standard <code>GridView<\/code> output:<\/p>\n<pre><code>public class CalendarActivity extends Activity {\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n\n    setContentView(R.layout.calendar);\n\n    GridView g = (GridView) findViewById(R.id.gridCalendar);\n    g.setAdapter(new EventGridAdapter(this, g));        \n}\n}\n<\/code><\/pre>\n<p>Layout:<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p><strong>What is the problem<\/strong>:<\/p>\n<p>When I run this code in emulator on ICS 4.0.3, grid is shown perfectly, but when I try to scroll it &#8211; program crashes with strange exception:<\/p>\n<p><code>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)<\/code><\/p>\n<p>When I run the same code in emulator on 2.3.3 &#8211; everything is fine!<\/p>\n<p>After some investigation I found the &#8220;source&#8221; of the problem (-. Look again at <code>getView<\/code> method in <code>EventGridAdapter<\/code>:<\/p>\n<pre><code>        int viewHeight = gridView.getHeight();\n        int spacing = 1;\n        int height = viewHeight \/ 5 - spacing; \n        cell.setMinimumHeight(height);\n<\/code><\/pre>\n<p>The problem line is:<\/p>\n<pre><code>cell.setMinimumHeight(height);\n<\/code><\/pre>\n<p>When I change it to:<\/p>\n<pre><code>cell.setMinimumHeight(10);\n<\/code><\/pre>\n<p>&#8230; crashes dissapear<\/p>\n<p>But I don&#8217;t know what do I do wrong? Everything looks logically correct: I need 5 rows. Also, making <code>height<\/code> lower doesn&#8217;t help (when all rows are in a view area, e.g. no scroll needed):<\/p>\n<pre><code>int height = viewHeight \/ 7 - spacing;\n<\/code><\/pre>\n<p>Your advices?<\/p>\n<ol>\n<li>\n<p>the problem is hear&#8230;<\/p>\n<pre><code>gridView.getHeight()\n<\/code><\/pre>\n<p><strong>why?<\/strong> because you will not have the height of the gridview @ the time it just created<\/p>\n<p>you can use this method in either <code>onWindowfocuschanged<\/code> or by using viewTreeobserver<\/p>\n<p>like the one below<\/p>\n<pre><code>ViewTreeObserver vto1 = gridView.getViewTreeObserver();\n        vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {\n\n            public void onGlobalLayout() {\n                \/\/ write hear for height\n\n                Log.i(TAG, \"from VTO\" + gridView.getHeight());\n                ViewTreeObserver obs = gridView.getViewTreeObserver();\n                obs.removeGlobalOnLayoutListener(this);\n            }\n        });\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:17:44. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;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; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1913","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1913"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1913\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}