findViewById crashing when using a custom view with <merge> and <include> tags-Collection of common programming errors
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’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.
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’m really not sure how to approach this, I can’t seem to get inflate to work either to test that instead. Any ideaS?
Thanks!
Game.java
setContentView(R.layout.test2);
View player1 = (View) findViewById(R.id.player_1);
TextView player1Name = (TextView) player1.findViewById(R.id.player_name);
player1Name.setText("John");
test2.xml
player_view.xml
This is a cut down version of player_view.xml, I can post more if needed.
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.
-
Your player_view.xml has unknown type, so the include is confused what kind of View is it. Try to do the next: Use or create new PlayerView class, mergeable with player_view.xml.
Edit test2.xml to:
then inflate it in your code as following:
setContentView(R.layout.test2); PlayerView player1 = (PlayerView) findViewById(R.id.player_1); LayoutInflater inflate = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflate.inflate(R.layout.player_view, player1);
Do the same for player2.