Я пытаюсь пройти через таблицу, чтобы проверить кнопку для условия, а затем изменить их на setText
. Проблема, с которой я столкнулась, до этого я получаю ClassCastException
. Я вижу, что он говорит, что я не могу наложить кнопку на ViewGroup, но я не уверен, почему это происходит, я не пытаюсь что-либо изменить в этот момент. Я считаю, что эта строка (69), является проблемой, но nty уверен, почему.AppCompatButton не может быть отнесен к android.view.ViewGroup
View view = ((ViewGroup) ((ViewGroup) tableLayoutChild).getChildAt(i));
Код:
public Button aiPlayerPick() {
Button btn = null;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) ((ViewGroup) tableLayoutChild).getChildAt(i));
if (view instanceof Button && view.getTag() == aiPickedButton) {
View btn_v = view.findViewWithTag(aiPickedButton);
System.out.println("Button: " + btn_v);
//btn = (Button) findViewById(R.id.btn_v);
break;
} else {
i++;
}
}
}
}
return btn;
}
Ошибка:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.view.ViewGroup
at com.example.richardcurteis.connect3.MainActivity.aiPlayerPick(MainActivity.java:69)
at com.example.richardcurteis.connect3.MainActivity.playerClick(MainActivity.java:49)
at com.example.richardcurteis.connect3.MainActivity.humanPlayerTurn(MainActivity.java:34)
at com.example.richardcurteis.connect3.MainActivity.receiveClick(MainActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
Совершенные. Приветствие –