我想在 if 条件中使用我的 PlayerClick(view v) 。但它给了我 null 异常。它必须检索按钮 ID。
//Main Code where i wan to call
while (gameLoop > 0){
while(PlayerClick(null) != 1){
if( WinnerCheck(currentUser.getSymbolValue()) == 1 || WinnerCheck(currentUser.getSymbolValue()) == 0 ){
return currentUser;
}
if (gameLoop % 2 == 0)
currentUser = me;
else
currentUser = opponent;
}
gameLoop--;
}
函数在这里
public int PlayerClick(View v) {
//
switch(v.getId()){
case R.id.btnone:
return -1;
case R.id.btntwo:
return -1;
}
return 1;
}
这样做的目的只是为了在单击右键时暂停 while 循环 我对 XML 中的四个按钮使用 android:onclick = "PlayerClick"
最佳答案
i want to use my PlayerClick(view v) inside a if condition . but it gives me null Exception.
因为您将 null
传递给 PlayerClick
方法。
要使其正常工作,您应该将用户按下的按钮 View 作为 PlayerClick
方法中的参数传递。
关于java - 如何在条件中使用 onClick 函数或在函数中调用它?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27861964/