Issue
I have a table with some buttons and TextView (called Row2Col1, Row2Col3, Row2Col4), and I would like to get their id from a string dinamically generated. The code is:
String index = "Row" + (currentRow + 2) + "Col1";
Button buttonTable = (Button) findViewById(getResources()
.getIdentifier(index, "id", getPackageName()));
buttonTable.setVisibility(0);
index = "Row" + (currentRow + 2) + "Col3";
TextView textViewBrand = new TextView(getApplicationContext());
textViewBrand = (TextView) findViewById(getResources().getIdentifier(
index, "id", getPackageName()));
textViewBrand.setText(brand);
index = "Row" + (currentRow + 2) + "Col4";
TextView textViewTread = new TextView(getApplicationContext());
textViewTread = (TextView) findViewById(getResources().getIdentifier(
index, "id", getPackageName()));
textViewTread.setText(tread);
The program crash at the second line, where is the findViewById. Any ideas?
Solution
According your error text, you have TextView in xml, but you are trying to cast it to Button in code. Change your code to
String index = "Row" + (currentRow + 2) + "Col1";
TextView buttonTable = (TextView) findViewById(getResources()
.getIdentifier(index, "id", getPackageName()));
buttonTable.setVisibility(0);
and it will works.
Answered By - Dimmerg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.