Почему я получаю исключение java.lang.IllegalStateException? Щелчок (Просмотр) находится в родительском элементеJAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Почему я получаю исключение java.lang.IllegalStateException? Щелчок (Просмотр) находится в родительском элементе

Сообщение Гость »


I got this error when I try to run the Tic Tac Toe game: java.lang.IllegalStateException: Could not find method click(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'btn_play_game'
I decided to keep the game code separately from the main activity (and also practice a little bit with creating intent).

Код: Выделить всё

*Main Activity.java*

package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Tic Tac Toe");
}
public void onClick(View view) {
Intent intent = new Intent(this,Game.class);
startActivity(intent);
}
}

*activity_main.xml*














*Game.java*

package com.example.tictactoe;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
public class Game extends AppCompatActivity implements View.OnClickListener {
boolean playerOneActive;
private TextView playerOneScore, playerTwoScore, playerStatus;
private final Button[] buttons = new Button[9];
private Button reset, playagain;
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
int rounds;
private int playerOneScoreCount, playerTwoScoreCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
playerOneScore = findViewById(R.id.score_Player1);
playerTwoScore = findViewById(R.id.score_Player2);
playerStatus = findViewById(R.id.textStatus);
reset = findViewById(R.id.btn_about_game);
playagain = findViewById(R.id.btn_play_game);
buttons[0] = findViewById(R.id.btn0);
buttons[1] = findViewById(R.id.btn1);
buttons[2] = findViewById(R.id.btn2);
buttons[3] = findViewById(R.id.btn3);
buttons[4] = findViewById(R.id.btn4);
buttons[5] = findViewById(R.id.btn5);
buttons[6] = findViewById(R.id.btn6);
buttons[7] = findViewById(R.id.btn7);
buttons[8] = findViewById(R.id.btn8);
for (Button button : buttons) {
button.setOnClickListener(this);
}
playerOneScoreCount = 0;
playerTwoScoreCount = 0;
playerOneActive = true;
rounds = 0;
}
@Override
public void onClick(View view) {
if (!((Button) view).getText().toString().equals("")) {
return;
} else if (checkWinner()) {
return;
}
String buttonID = view.getResources().getResourceEntryName(view.getId());
int gameStatePointer = Integer.parseInt(buttonID.substring(buttonID.length() - 1));
if (playerOneActive) {
view.setOnContextClickListener((View.OnContextClickListener) ContextCompat.getDrawable(this, R.drawable.x_sign));
gameState[gameStatePointer] = 0;
} else {
view.setOnContextClickListener((View.OnContextClickListener) ContextCompat.getDrawable(this, R.drawable.o_sign));
gameState[gameStatePointer] = 1;
}
rounds++;
if (checkWinner()) {
if (playerOneActive) {
playerOneScoreCount++;
updatePlayerScore();
playerStatus.setText("Player 1 has won");
} else {
playerTwoScoreCount++;
updatePlayerScore();
playerStatus.setText("Player 2 has won");
}
} else if (rounds == 9) {
playerStatus.setText("Nobody has won.");
} else {
playerOneActive = !playerOneActive;
}
reset.setOnClickListener(view1 -> {
playAgain();
playerOneScoreCount = 0;
playerTwoScoreCount = 0;
updatePlayerScore();
});
playagain.setOnClickListener(view12 -> playAgain());
}
private boolean checkWinner() {
boolean winnerResults = false;
for (int[] winningPositions : winningPositions) {
if (gameState[winningPositions[0]] == gameState[winningPositions[1]] && gameState[winningPositions[1]] == gameState[winningPositions[2]] && gameState[winningPositions[0]] != 2) {
winnerResults = true;
break;
}
}
return winnerResults;
}
private void playAgain() {
rounds = 0;
playerOneActive = true;
for (int i = 0; i < buttons.length;  i++) {
gameState[i] = 2;
buttons[i].setText("");
}
playerStatus.setText("Status");
}
private void updatePlayerScore() {
playerOneScore.setText(Integer.toString(playerOneScoreCount));
playerTwoScore.setText(Integer.toString(playerTwoScoreCount));
}
}

*activity_game.xml*








         & l t ; / R e l a t i v e L a y o u t & g t ; < b r   / >         & l t ; L i n e a r L a y o u t < b r   / >                 a n d r o i d : i d = & q u o t ; @ + i d / l a y o u t _ l i n e a r & q u o t ; < b r   / >                 a n d r o i d : l a y o u t _ w i d t h = & q u o t ; w r a p _ c o n t e n t & q u o t ; < b r   / >                 a n d r o i d : l a y o u t _ h e i g h t = & q u o t ; w r a p _ c o n t e n t & q u o t ; < b r   / >                 a n d r o i d : l a y o u t _ b e l o w = & q u o t ; @ i d / r e l a t i v e _ l a y o u t & q u o t ; < b r   / >                 a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = & q u o t ; t r u e & q u o t ; < b r   / >                 a n d r o i d : o r i e n t a t i o n = & q u o t ; v e r t i c a l & q u o t ; & g t ; < b r   / >                 & l t ; L i n e a r L a y o u t < b r   / >                         a n d r o i d : l a y o u t _ w i d t h = & q u o t ; w r a p _ c o n t e n t & q u o t ; < b r   / >                         a n d r o i d : l a y o u t _ h e i g h t = & q u o t ; w r a p _ c o n t e n t & q u o t ; < b r   / >                         a n d r o i d : o r i e n t a t i o n = & q u o t ; h o r i z o n t a l & q u o t ; & g t ; < b r   / >                         & l t ; B u t t o n < b r   / >                                 a n d r o i d : i d = & q u o t ; @ + i d / b t n 0 & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ w i d t h = & q u o t ; 7 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ h e i g h t = & q u o t ; 7 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ m a r g i n H o r i z o n t a l = & q u o t ; 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ m a r g i n B o t t o m = & q u o t ; 1 . 2 5 d p & q u o t ; < b r   / >                                 a n d r o i d : b a c k g r o u n d T i n t = & q u o t ; @ c o l o r / b l a c k & q u o t ; < b r   / >                                 a n d r o i d : t e x t C o l o r L i n k = & q u o t ; # F 8 F 6 F 6 & q u o t ; < b r   / >                                 a n d r o i d : t e x t S i z e = & q u o t ; 2 6 s p & q u o t ; < b r   / >                                 a n d r o i d : d r a w a b l e E n d = & q u o t ; @ d r a w a b l e / x o _ 4 8 x 4 8 & q u o t ; / & g t ; < b r   / >                         & l t ; B u t t o n < b r   / >                                 a n d r o i d : i d = & q u o t ; @ + i d / b t n 1 & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ w i d t h = & q u o t ; 7 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ h e i g h t = & q u o t ; 7 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ m a r g i n H o r i z o n t a l = & q u o t ; 5 d p & q u o t ; < b r   / >                                 a n d r o i d : l a y o u t _ m a r g i n B o t t o m = & q u o t ; 1 . 2 5 d p & q u o t ; < b r   / >                                 a n d r o i d : b a c k g r o u n d T i n t = & q u o t ; @ c o l o r / b l a c k & q u o t ; < b r   / >                       android:textSize="26sp"
android:drawableRight="@drawable/xo_48x48"/>



















Источник: https://stackoverflow.com/questions/781 ... n-a-parent
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»