{"id":2239,"date":"2022-08-30T15:23:12","date_gmt":"2022-08-30T15:23:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/android-runtime-error-when-adding-imageview-to-view-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:12","modified_gmt":"2022-08-30T15:23:12","slug":"android-runtime-error-when-adding-imageview-to-view-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-runtime-error-when-adding-imageview-to-view-collection-of-common-programming-errors\/","title":{"rendered":"Android runtime error when adding imageview to view-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m having a runtime error when I&#8217;m adding an image to my main view\/layout ( I tried with both ).<\/p>\n<p>Code starts from an activity and I create the game board no problem, when it&#8217;s done the game start, all this goes well. The issue occurs when I press a button to add an image to the screen, I get a run time error at the moment the view is added into the main view. In my code I&#8217;m keeping everything Java only, I&#8217;m avoiding XML files, I&#8217;m wondering if that might be a part of the problem. I have been reading some solution to similar problem where people suggested to use the layout inflater method however I cannot find reference to a use of this method without having an XML file.<\/p>\n<p>Here is my code<\/p>\n<p>(PS sorry I see in the preview window how wierd it breaks at some moments&#8230; the error log is at the end<\/p>\n<pre><code>     RelativeLayout GameView = new RelativeLayout(this);\n     GameView.setLayoutParams(new LayoutParams(\n             screenHei,\n             screenLen));\n\n\n\n\n     \/\/Start Game sequence\n     ZoomView layout3 = new ZoomView(this);\n\n\n     layout3.setLayoutParams(new LayoutParams(\n             screenHei,\n             screenLen));\n\n\n     \/\/Relative layout... aka base view that will be inserted in the zoom view\n     RelativeLayout baseBoard = new RelativeLayout(this);\n     \/\/LinearLayOut Setup\n\n\n     baseBoard.setLayoutParams(new LayoutParams(\n             screenHei,\n             screenLen));\n\n\n        LinearLayout linearLayout= new LinearLayout(this);\n\n        linearLayout.setOrientation(LinearLayout.VERTICAL);\n\n        linearLayout.setLayoutParams(new LayoutParams(\n                 screenHei,\n                 screenLen));\n        \/\/ImageView Setup\n        ImageView imageView = new ImageView(this);\n        \/\/setting image resource\n        imageView.setImageResource(R.drawable.bluepage);\n        \/\/setting image position\n\n        imageView.setLayoutParams(new LayoutParams(\n                 screenHei,\n                 screenLen));\n        \/\/adding view to layout\n        imageView.setScaleType(ImageView.ScaleType.FIT_XY);\n        linearLayout.addView(imageView);\n        \/\/make visible to program\n        linearLayout.setX(0);\n        linearLayout.setY(0);\n\/\/   RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(\n\/\/              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);\n\/\/      lp.addRule(RelativeLayout.BELOW, v.getId());\n\n        baseBoard.addView(linearLayout); \n     layout3.setX(0);\n     layout3.setY(0);\n\n\n\n        final RelativeLayout menuMobile = new RelativeLayout(this);\n\n\n         menuMobile.setLayoutParams(new LayoutParams(\n                 200,\n                 200));\n         menuMobile.setBackgroundColor(0xFF000000 );\n\n         Button ButtonMobile1 = new Button(this);\n         ButtonMobile1.setBackgroundColor(0xFFCCFFCC);\n         ButtonMobile1.setText(\"test1\");\n         ButtonMobile1.setTextSize(6);\n         ButtonMobile1.setLayoutParams(new LayoutParams(155,66));\n         ButtonMobile1.setX(100);\n         ButtonMobile1.setY(40);\n         menuMobile.addView(ButtonMobile1); \n         ButtonMobile1.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View view) {\n\n                 Log.v(\"prepare to add barrack! \",\" testing Barrack \");\n                 menuMobile.setVisibility(View.INVISIBLE);\n\n                 @SuppressWarnings(\"rawtypes\")\n                ArrayList aList = (ArrayList) GroundWork.get(selectedX);\n                 GameTile axs = (GameTile) aList.get(selectedY);\n\n                   axs.selected=1;\n                   axs.createBuilding(1,1,1);\n\n\n                   addPicture(axs.imgSizeIds);\n\n\n                   Log.v(\"tile selected value and information \",\" axs.selected :\" + axs.selected);\n                }\n            });\n\n\n     valueX=21;\n     valueY=12;\n     \/\/Base map builder\n     for(int l=0; l<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:31:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having a runtime error when I&#8217;m adding an image to my main view\/layout ( I tried with both ). Code starts from an activity and I create the game board no problem, when it&#8217;s done the game start, all this goes well. The issue occurs when I press a button to add an image [&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-2239","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2239","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=2239"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2239\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}