{"id":2323,"date":"2022-08-30T15:23:54","date_gmt":"2022-08-30T15:23:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/why-does-populating-a-listview-from-sql-crashes-my-android-activity-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:54","modified_gmt":"2022-08-30T15:23:54","slug":"why-does-populating-a-listview-from-sql-crashes-my-android-activity-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-does-populating-a-listview-from-sql-crashes-my-android-activity-collection-of-common-programming-errors\/","title":{"rendered":"Why does populating a &lt;ListView&gt; from SQL crashes my Android activity?-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to do an example from the Android Developer&#8217;s Cookbook to create and populate an ListView with entries from a SQLite file.<\/p>\n<p>Unfortunately I can not find the bug in my code. The activity is crashing right after I reference the R.layout.diaries. Can anyone tell me where my problem is, and why I am crashing with my layout?<\/p>\n<p>Java file:<\/p>\n<pre><code>public class DisplayDiaries extends ListActivity{\n    MyDB dba;\n    DiaryAdapter myAdapter;\n\n    private class MyDiary { \n        public MyDiary(String t, String c, String r){\n            title = t;\n            content = c;\n            recorddate= r;\n        }\n        public String title;\n        public String content;\n        public String recorddate;\n    }\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState){\n        Log.i(\"DisplayDiaries\", \"started onCreate\");\n        dba = new MyDB(this);\n        dba.open();\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.diaries);\n    \/\/I am crashing here\n        Log.i(\"DisplayDiaries\", \"layout complite\");\n\n        myAdapter = new DiaryAdapter(this);\n        this.setListAdapter(myAdapter);\n    }\n    private class DiaryAdapter extends BaseAdapter {\n        private LayoutInflater mInflater;\n        private ArrayList diaries;\n        public DiaryAdapter (Context context) {\n            mInflater = LayoutInflater.from(context);\n            diaries = new ArrayList();\n            getdata();\n        }\n        public void getdata(){\n            Cursor c = dba.getdiaries();\n            startManagingCursor(c);\n            if(c.moveToFirst()){\n                do{\n                    String title = c.getString(c.getColumnIndex(Constants.TITLE_NAME));\n                    String content = c.getString(c.getColumnIndex(Constants.CONTENT_NAME));\n                    DateFormat dateFormat = DateFormat.getDateTimeInstance();\n                    String datedata = dateFormat.format(new Date(c.getLong(c.getColumnIndex(Constants.DATE_NAME))).getTime());\n                    MyDiary temp = new MyDiary(title,content,datedata);\n                    diaries.add(temp);\n                }while(c.moveToNext());\n            }\n        }\n\n        @Override\n        public int getCount() {\n            return diaries.size();\n        }\n\n        @Override\n        public MyDiary getItem(int i) {\n            return diaries.get(i);\n        }\n\n        @Override\n        public long getItemId(int i) {\n            return i;\n        }\n\n        @Override\n        public View getView(int arg0, View arg1, ViewGroup arg2) {\n            final ViewHolder holder;\n            View v = arg1;\n            if ((v==null) || (v.getTag() == null)) {\n                v = mInflater.inflate(R.layout.diaryrow, null);\n                holder = new ViewHolder();\n                holder.mTitle =(TextView)v.findViewById(R.id.name);\n                holder.mDate = (TextView)v.findViewById(R.id.datetext);\n                v.setTag(holder);\n            } else {\n                holder = (ViewHolder) v.getTag();\n            }\n\n            holder.mdiary = getItem(arg0);\n            holder.mTitle.setText(holder.mdiary.title);\n            holder.mDate.setText(holder.mdiary.recorddate);\n\n            v.setTag(holder);\n            return v;\n        }\n\n        public class ViewHolder {\n            MyDiary mdiary;\n            TextView mTitle;\n            TextView mDate;\n        }\n    }\n}\n<\/code><\/pre>\n<p>diaries.xml file:<\/p>\n<pre><code>\n\n\n\n\n<\/code><\/pre>\n<p>diaryrow.xml file:<\/p>\n<pre><code>\n    \n    \n    \n         \n\n<\/code><\/pre>\n<ol>\n<li>\n<p>I am guessing that it bombs out because you have commented out the TextView in diaries.xml, which should by the way have an ID of @android:id\/empty.<\/p>\n<\/li>\n<li>\n<p>Hey even i have been working on the same database model&#8230; even i had to face the same problem&#8230;wherein it couldn&#8217;t resolve R.layout.history Note: history.xml file has the same content as the dairies.xml file just the name is different<\/p>\n<p>what i did was i recreated the same file with the same content&#8230; by right clicking the layout folder&#8211;&gt;new&#8211;&gt;Android XML File<\/p>\n<p>Then again while entering the path(as in R.layout.history in my case) were initially the error was occuring&#8230; try to go step by step by typeing in R.layout. and then let the eclipse drop box appear then select the file dairies&#8230;<\/p>\n<p>if you still cant get it then try checking your R.java(do no modify it!).<\/p>\n<\/li>\n<li>\n<p>The ID for the ListView in diaries.xml should be @android:id\/list (Also, in diaryrow.xml, you should move android:layout_alignLeft and android:layout_below from the RelativeLayout element to the second TextView element)<\/p>\n<\/li>\n<li>\n<p>I&#8217;m not sure if this is it, but in my listview activity xml, the id of the listview is:<\/p>\n<pre><code>android:id=\"@android:id\/list\"\n<\/code><\/pre>\n<p>and not<\/p>\n<pre><code>android:id=\"@+id\/list\"\n<\/code><\/pre>\n<p>I&#8217;m really new to android, but could that be it?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 10:10:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to do an example from the Android Developer&#8217;s Cookbook to create and populate an ListView with entries from a SQLite file. Unfortunately I can not find the bug in my code. The activity is crashing right after I reference the R.layout.diaries. Can anyone tell me where my problem is, and why I [&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-2323","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2323","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=2323"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2323\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}