简体   繁体   English

实例变量在java中神奇地分配

[英]Instance variable gets magically assigned in java

I have been trying to fix a bug in my application for already 3 days, with no success.我已经尝试修复应用程序中的错误 3 天了,但没有成功。 The problem is that an instance variable gets magically assigned.问题是实例变量被神奇地分配了。

The application is a bluetooth multiplayer game in which the players take turn.该应用程序是一个蓝牙多人游戏,玩家轮流进行。 After the game is over a dialog is shown to ask the players if they would like to continue.游戏结束后,会显示一个对话框,询问玩家是否愿意继续。 So when the player 1 continues, a message is sent to player 2 to notify that player 1 wants continue.因此,当玩家 1 继续时,会向玩家 2 发送一条消息,通知玩家 1 想要继续。 The variable "opponentContinued" gets true.变量“opponentContinued”变为真。 And when the player 2 clicks continue too, if opponentContinued equals true the game restarts.并且当玩家 2 也点击继续时,如果对方继续为真,则游戏重新开始。

The problem :问题 :

When the dialog is shown to both of the players and player 1 rotates the screen, player 2 clicks continue, player 1 receives notification and "opponentContinued" gets assigned as true.当对话显示给两个玩家并且玩家 1 旋转屏幕时,玩家 2 单击继续,玩家 1 收到通知并且“opponentContinued”被指定为 true。 AFTER this when player 1 clicks continue and (opponentContinued must be true) checks if "opponentContinued" is true, finds that "opponentContinued" is false.在此之后,当玩家 1 单击继续并(opponentContinued 必须为 true)检查“opponentContinued”是否为 true 时,发现“opponentContinued”为 false。

Everything works fine if there is no rotation.如果没有旋转,一切正常。

Then to make it a bit clear I made a dummy variable to monitor when and how "opponentContinued" gets assigned and every time it got assigned I made dummyBoolean equal to "opponentContinued".然后为了更清楚一点,我创建了一个虚拟变量来监视“opponentContinued”何时以及如何被分配,每次分配时我都将 dummyBoolean 设置为等于“opponentContinued”。 I made dummyBoolean true as default.我将 dummyBoolean 设为默认值。 In "onContinueClicked" methods "dummyBoolean" is true, while "opponentContinued" is false.在“onContinueClicked”方法中,“dummyBoolean”为真,而“opponentContinued”为假。

public class MainActivity extends Activity {

    private boolean opponentContinued;
    private boolean dummyBoolean = true;



    private void onOpponentContinued(){
        opponentContinued = true;
        dummyBoolean = opponentContinued;
    }

    private void onContinueClicked(){
        // opponentContinued is false
        // dummyBoolean is true
        if (opponentContinued){
              // Continue the game
        }
    }
}

I have no idea what could be the problem.我不知道可能是什么问题。 Have anyone experienced a problem like this?有没有人遇到过这样的问题? Any help is appreciated.任何帮助表示赞赏。

EDIT编辑

Please note that the problem has to do nothing with saving states during screen rotation.请注意,该问题与在屏幕旋转期间保存状态无关。 The problem is that the variable "opponentContined" is "magically" assigned to the default value false, after it is set to true.问题是变量“opponentContined”在设置为true 后被“神奇地”分配给默认值false。

I know this sounds unrealistic but it has ruined me a lot of time.我知道这听起来不切实际,但它毁了我很多时间。

https://developer.android.com/guide/topics/resources/runtime-changes.html https://developer.android.com/guide/topics/resources/runtime-changes.html

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language).某些设备配置可能会在运行时更改(例如屏幕方向、键盘可用性和语言)。 When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()).当发生这样的变化时,Android 会重新启动正在运行的 Activity(调用 onDestroy(),然后是 onCreate())。 The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.重启行为旨在通过使用与新设备配置匹配的替代资源自动重新加载您的应用程序,从而帮助您的应用程序适应新配置。

To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state.为了正确处理重启,重要的是您的 Activity 通过正常的 Activity 生命周期恢复其先前的状态,其中 Android 在销毁您的 Activity 之前调用 onSaveInstanceState() 以便您可以保存有关应用程序状态的数据。 You can then restore the state during onCreate() or onRestoreInstanceState()然后您可以在 onCreate() 或 onRestoreInstanceState() 期间恢复状态

Your problem is that when the user rotates the device, your activity object is destroyed and a new one is created in its place.您的问题是,当用户旋转设备时,您的活动对象将被销毁,并在其位置创建一个新对象。 Thus the values are (re-)initialized during the creation of the new instance.因此,在创建新实例期间(重新)初始化这些值。 You need to save the state and then restore it when the device is rotated.您需要保存状态,然后在设备旋转时恢复它。

When you are checking if the opponent continued, you should at first ask for a variable content.当你检查对手是否继续时,你首先应该要求一个可变的内容。 If opponentContinued is false, repeat the question to the opponent's device.如果opponentContinued为假,则对对方的设备重复该问题。

Default value of boolean in java is false since variable is being recreated assigned by default value false java 中布尔值的默认值为 false,因为正在重新创建由默认值 false 分配的变量

The problem is Activity is getting recreated on screen orientation changed.you can solve this by 3 ways问题是 Activity 在屏幕方向改变时重新创建。你可以通过 3 种方法解决这个问题

1.you can make use of onSaveInstanceState and onRestoreInstanceState to save and retrieve boolean value 1.你可以利用onSaveInstanceState和onRestoreInstanceState来保存和检索布尔值

2.make your boolean variable static and declare it outside lifecycle methods 2.使您的布尔变量静态并在生命周期方法之外声明它

3.use shared preferences to keep track of your boolean variable 3.使用共享首选项来跟踪您的布尔变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM