java - 如何在条件中使用 onClick 函数或在函数中调用它?安卓

标签 java android android-studio

我想在 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/

相关文章:

java - 无法通过 Intent 传递 Parcelable

android - 如何将 AirPush 广告集成到我的 Android 应用程序中?

android - Mockito vs 类的测试实现

java.io.FileNotFoundException : open failed: EROFS (Read-only file system) 错误

java - Firebase - 重复插入数据库

java - 如何将 edittext 值传递给另一个 Activity 的 TextView

java - 为什么不允许使用 volatile final 字段?

android - 指南针精度困境

android - "android:windowLightStatusBar"不工作

android - Linux 上更快的 android 模拟器