无法启动活动ComponentInfo {com.example.sulejo9.gato / com.example.sulejo9.gato.MainActivity}:java.lang.NullPointerException:尝试调用虚方法' void android.view.View。 setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上
我收到上面的错误,我不明白如何实现我的set onclicklistener,对于[x] [y]
中的所有按钮我似乎没有看到为什么buttonreset是我的变量,当我在这行中调用它时
private Button[][] buttons = new Button[3][3];
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button[][] buttons = new Button[3][3];
private TextView textViewPlayer;
private TextView textViewNpc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Referencias a los textview
textViewPlayer = findViewById(R.id.text_view_Player);
textViewNpc = findViewById(R.id.text_view_NPC);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
String buttonID = "button_" + x + y;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName()); //5:08
buttons[x][y] = findViewById(resID);
buttons[x][y].setOnClickListener(this);
}
}
Button buttonReset = findViewById(R.id.button_reset);
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}