0

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

<include layout="@layout/player_view" android:id="@+id/player_1" /> <include layout="@layout/player_view" android:id="@+id/player_2" /> 

player_view.xml

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:player="http://schemas.android.com/apk/res/com.example.android.merge"> <LinearLayout android:id="@+id/top_view" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" > <TextView android:id="@+id/player_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" > </TextView> </LinearLayout> </merge> 

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.

1
  • If you can explain bit more,what exactly you want to do ? Your TextView player_name is in a Linear layout top_view, but what the point of getting the player_1 View ?? Commented Jun 12, 2011 at 17:44

1 Answer 1

1

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:

<PlayerView android:id="@+id/player_1" /> <PlayerView android:id="@+id/player_2" /> 

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.