{"id":2269,"date":"2022-08-30T15:23:27","date_gmt":"2022-08-30T15:23:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/findviewbyid-crashing-when-using-a-custom-view-with-merge-and-include-tags-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:27","modified_gmt":"2022-08-30T15:23:27","slug":"findviewbyid-crashing-when-using-a-custom-view-with-merge-and-include-tags-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/findviewbyid-crashing-when-using-a-custom-view-with-merge-and-include-tags-collection-of-common-programming-errors\/","title":{"rendered":"findViewById crashing when using a custom view with &lt;merge&gt; and &lt;include&gt; tags-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to make a custom view using XML then retrieve its properties and edit them. This custom view is added into the test2 view several times with different id&#8217;s for each. I have tried to retrieve the id for one of these custom views (player_1) then get the TextView (player_name), and edits its properties and no other views like the player_2 id view.<\/p>\n<p>But I am not sure that this is this even possible? It renders fine, but I get an error every time I try to do this in code. I&#8217;m really not sure how to approach this, I can&#8217;t seem to get inflate to work either to test that instead. Any ideaS?<\/p>\n<p>Thanks!<\/p>\n<p>Game.java<\/p>\n<pre><code>setContentView(R.layout.test2);\n\nView player1 = (View) findViewById(R.id.player_1);\n\nTextView player1Name = (TextView) player1.findViewById(R.id.player_name);\n\nplayer1Name.setText(\"John\");\n<\/code><\/pre>\n<p>test2.xml<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>player_view.xml<\/p>\n<pre><code>\n\n\n        \n        \n   \n\n<\/code><\/pre>\n<p>This is a cut down version of player_view.xml, I can post more if needed.<\/p>\n<p>Also I have not worked out if it even prints the error message for this in eclipse, it goes into debug mode and does not explain any further what the issue is.<\/p>\n<ol>\n<li>\n<p>Your player_view.xml has unknown type, so the <strong>include<\/strong> is confused what kind of View is it. Try to do the next: Use or create new PlayerView class, mergeable with player_view.xml.<\/p>\n<p>Edit test2.xml to:<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>then inflate it in your code as following:<\/p>\n<pre><code>setContentView(R.layout.test2);\n\nPlayerView player1 = (PlayerView) findViewById(R.id.player_1);\nLayoutInflater inflate = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);\ninflate.inflate(R.layout.player_view, player1);\n<\/code><\/pre>\n<p>Do the same for player2.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:41:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to make a custom view using XML then retrieve its properties and edit them. This custom view is added into the test2 view several times with different id&#8217;s for each. I have tried to retrieve the id for one of these custom views (player_1) then get the TextView (player_name), and edits its [&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-2269","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2269","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=2269"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}