{"id":2299,"date":"2022-08-30T15:23:42","date_gmt":"2022-08-30T15:23:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/android-why-is-my-linearlayout-crashing-when-switching-at-runtime-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:42","modified_gmt":"2022-08-30T15:23:42","slug":"android-why-is-my-linearlayout-crashing-when-switching-at-runtime-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-why-is-my-linearlayout-crashing-when-switching-at-runtime-collection-of-common-programming-errors\/","title":{"rendered":"Android: why is my linearlayout crashing when switching at runtime?-Collection of common programming errors"},"content":{"rendered":"<p>when user presses a button i would like to switch to the &#8216;details&#8217; layout which is like this:<\/p>\n<pre><code>\n\n\n\n              \n\n        \n\n\n<\/code><\/pre>\n<p>and this is how i am trying to switch it:<\/p>\n<pre><code> switch(item.getItemId())\n                {\n                case ID_DETAILS:\n                    \/\/ show new layout to for details                   \n                    LinearLayout detailsLayout = (LinearLayout) findViewById(R.id.layoutDetails);\n                    LayoutInflater detailsvi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n                    View detailsv = detailsvi.inflate(R.layout.activity_details, null);\n\n\/\/ IT CRASHES ON THIS LINE\n                    detailsLayout.addView(detailsv,new LinearLayout.LayoutParams(detailsLayout.getLayoutParams().width, detailsLayout.getLayoutParams().height));\n\n                            return true;\n}\n<\/code><\/pre>\n<p>It gives a null pointer exception error!!!<\/p>\n<pre><code>08-09 17:23:47.146: E\/AndroidRuntime(1572): FATAL EXCEPTION: main\n08-09 17:23:47.146: E\/AndroidRuntime(1572): java.lang.NullPointerException\n<\/code><\/pre>\n<ol>\n<li>\n<p>Here is how you solve this:<\/p>\n<p>1) put a breakpoint on the crashline and run in debug mode. You can immediately see if detailsLayout is null or not. If not do step 2 Also check if detailsv is null.<\/p>\n<p>2) details Object o1 = etailsLayout.getLayoutParams(); \/\/ check if this is null.<\/p>\n<p>It has to be one or the other.<\/p>\n<p>Update: Since its detailsLayout is in fact null. It must be that either<\/p>\n<p>1) setContentView was not called prior to requsting detailsLayout, or 2) setContentView was set to a layout that did not define detailsLayout, or 3) setContentView was called correctly but detailsLayout is not correctly id in that layout file.<\/p>\n<p>It must be one of these three.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:58:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>when user presses a button i would like to switch to the &#8216;details&#8217; layout which is like this: and this is how i am trying to switch it: switch(item.getItemId()) { case ID_DETAILS: \/\/ show new layout to for details LinearLayout detailsLayout = (LinearLayout) findViewById(R.id.layoutDetails); LayoutInflater detailsvi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View detailsv = detailsvi.inflate(R.layout.activity_details, 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-2299","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2299","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=2299"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2299\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}