{"id":1862,"date":"2022-08-30T15:20:04","date_gmt":"2022-08-30T15:20:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/custom-dialog-with-a-listview-crashing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:04","modified_gmt":"2022-08-30T15:20:04","slug":"custom-dialog-with-a-listview-crashing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/custom-dialog-with-a-listview-crashing-collection-of-common-programming-errors\/","title":{"rendered":"Custom Dialog with a listview crashing-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve tried different things but things are still not &#8216;going&#8217;<\/p>\n<p>I have an activity. In this, when I press one of the options in the menu, I need to popup a dialog and in that dialog a listview of string values wich I get out of an string-arrays xml.<\/p>\n<p>I&#8217;ve already deleted some code to clean it up but still no luck&#8230;<\/p>\n<p>myList.xml<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>my custom dialog<\/p>\n<pre><code>public class Selector extends Dialog {\n\nString[] testArray;\nprotected Selector(Context context) {\n    super(context);\n\n    ListView lijstje = (ListView) findViewById(R.layout.mylist);\n    testArray = context.getResources().getStringArray(R.array.currencies);\n    Log.d(\"test\",testArray[0]);\/\/wich shows me the 1e string and it's working\n    ArrayAdapter adapter = new ArrayAdapter(this.getContext(),android.R.layout.simple_list_item_1, testArray);\n    lijstje.setAdapter(adapter);\n\n    LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n    View v = li.inflate(R.layout.mylist, null, false);\n    this.setContentView(v);\n\n}\n\n}\n<\/code><\/pre>\n<p>in my main activity I have I function<\/p>\n<pre><code>public void showPopup()\n{       \n    Selector test = new Selector(this);\n    test.show();\n}\n<\/code><\/pre>\n<p>when I put lijstje.setAdapter(adapter); in comment my app is not crashing but the dialog is very small and empty<\/p>\n<pre><code>02-24 22:14:29.705: D\/AndroidRuntime(28734): Shutting down VM\n02-24 22:14:29.705: W\/dalvikvm(28734): threadid=1: thread exiting with uncaught exception (group=0x40a4e1f8)\n02-24 22:14:29.715: E\/AndroidRuntime(28734): FATAL EXCEPTION: main\n02-24 22:14:29.715: E\/AndroidRuntime(28734): java.lang.NullPointerException\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.showPopup(OmzetterActivity.java:83)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.onOptionsItemSelected(OmzetterActivity.java:59)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.app.Activity.onMenuItemSelected(Activity.java:2502)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.widget.AbsListView.performItemClick(AbsListView.java:1058)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.widget.AbsListView$1.run(AbsListView.java:3168)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.os.Handler.handleCallback(Handler.java:605)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.os.Handler.dispatchMessage(Handler.java:92)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.os.Looper.loop(Looper.java:137)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at android.app.ActivityThread.main(ActivityThread.java:4424)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at java.lang.reflect.Method.invokeNative(Native Method)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at java.lang.reflect.Method.invoke(Method.java:511)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)\n02-24 22:14:29.715: E\/AndroidRuntime(28734):    at dalvik.system.NativeStart.main(Native Method)\n<\/code><\/pre>\n<ol>\n<li>\n<p>I&#8217;m not sure this will magically solve all you&#8217;re problems, but in your Selector constructor you have the following assignment:<\/p>\n<pre><code>ListView lijstje = (ListView) findViewById(R.layout.mylist);\n<\/code><\/pre>\n<p>Note how you are trying to find a View with a layout id. It should say something like <code>R.id.mylist<\/code> in stead. Currently it&#8217;ll resolve to <code>null<\/code>, hence the <code>NullPointerException<\/code>.<\/p>\n<p>Also, you&#8217;re doing <code>setContentView(...)<\/code> all the way at the end. If you&#8217;re trying to inflate views from the mentioned layout file (<code>R.layout.mylist<\/code>), make sure you do this <strong>after<\/strong> having set the content view.<\/p>\n<p>By the way, if you don&#8217;t have a fancy layout for the dialog displaying a list, you can probably suffice with the default AlertDialog builder. Have a read here, samples included. For some help on custom dialogs, look further down the bottom of that same page.<\/p>\n<\/li>\n<li>\n<p>I&#8217;m guessing that this is your problem. There is nothing to find the view by<\/p>\n<pre><code>ListView lijstje = (ListView) findViewById(R.layout.mylist);\n<\/code><\/pre>\n<p>lijstje is null when called this way lijstje is a layout not a view<\/p>\n<p>also set content view should be before anything else<\/p>\n<pre><code>public class Selector extends Dialog {\n\nString[] testArray;\nprotected Selector(Context context) {\n    super(context);\n    setContentView(R.layout.list);\n\n    ListView lijstje = (ListView) findViewById(R.id.mylist);\n    testArray = context.getResources().getStringArray(R.array.currencies);\n    Log.d(\"test\",testArray[0]);\/\/wich shows me the 1e string and it's working\n    ArrayAdapter adapter = new ArrayAdapter (this.getContext(),android.R.layout.simple_list_item_1, testArray);\n    lijstje.setAdapter(adapter);\n}\n<\/code><\/pre>\n<p>mylist.xml<\/p>\n<pre><code>\n\n    \n\n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>check the following links dialogwithlistview stackoverflow anddev<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:02:14. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve tried different things but things are still not &#8216;going&#8217; I have an activity. In this, when I press one of the options in the menu, I need to popup a dialog and in that dialog a listview of string values wich I get out of an string-arrays xml. I&#8217;ve already deleted some code to [&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-1862","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1862","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=1862"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1862\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}