我的变量值在按钮点击时神奇地变为0

时间:2015-12-11 22:50:29

标签: android

起初我认为这是Parse的一个问题,但后来我开始记录我发送到云端的变量的值。由于某种原因,该值为0.我有一个导引条,当条形滑动时,它会更新变量的值。我有一个日志声明,显示值已更改,因此我知道在按钮单击之前我的代码没有任何问题。

然后点击一个按钮,该变量的值将被发送到Parse。我在点击按钮时记录了值,变量包含值0.我不知道为什么会发生这种情况。我知道变量在单击按钮之前有一个值。然后以某种方式操纵价值

KEYS *
1) ":1:asdf"
2) ":1:django.contrib.sessions.cacheg2l0bo9z88z8bn4q2ep0andjgo8zrzzk"

修改

我有两个导向条,它们正在更新两个不同的变量。其中一个变量将始终具有值,而另一个变量的值为0.每次单击按钮时,值0都会在每个变量之间交替变换。这是我以前从未见过的事情

修改

public class SettingsScreen extends AppCompatActivity {

 ... //////// ommitted code /////

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_screen);
    ButterKnife.bind(this);

    Intent intent = getIntent();
    isLoggedIn = intent.getBooleanExtra("currentUser", false);


    if (isLoggedIn) {
        userIsLoggedIn();
    } else {

          ... //////// ommitted code /////

     }

    applyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isLoggedIn) {
                mUser.put("WORK", mDesiredWorkTime);
                mUser.put("REST", mDesiredRestTime);
                mUser.put(ACCESSEDSETTINGS, true); //when user clicks apply, ACCESSEDSETTINGS will be true and so the if statement in the method below wont run
                mUser.saveInBackground();



                Log.d(TAG, "putting desired user settings into database");
                Log.d(TAG, "desired work time: " + mDesiredWorkTime);  // SOMETIMES THIS IS ZERO???  
                Log.d(TAG, "desired rest time " + mDesiredRestTime);   // AND SOMETIMES THIS IS ZERO??? 



            }

            Intent intent = new Intent(SettingsScreen.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra(LOCALWORK, mDesiredWorkTime);
            intent.putExtra(LOCALREST, mDesiredRestTime);
            startActivity(intent);
        }
    });
}


 public void userIsLoggedIn() {

    mUser = ParseUser.getCurrentUser();
    mObjectId = mUser.getObjectId();
    registeredUserAccessedSettings = mUser.getBoolean(ACCESSEDSETTINGS);

    Log.d(TAG, "Has user accessed settings before? " + registeredUserAccessedSettings);

    if (!registeredUserAccessedSettings) { //this code will run first time user accesses settings with parse

        workFromDatabase = 25;
        restFromDatabase = 5;

        Log.d(TAG, "launched settings with account for first time");

    } else {
        workFromDatabase = mUser.getInt("WORK");
        restFromDatabase = mUser.getInt("REST");

        workText.setText("Current: " + workFromDatabase + " minutes");
        restText.setText("Current: " + restFromDatabase + " minutes");

        Log.d(TAG, "got user settings from database");
        Log.d(TAG, "desired work time: " + workFromDatabase);
        Log.d(TAG, "desired rest time " + restFromDatabase);
    }


    workBar.setMax(MAX_WORK - MIN_WORK);
    workBar.setProgress(workFromDatabase - MIN_WORK);

    workBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            mDesiredWorkTime = MIN_WORK + progress;
            workText.setText("Current: " + mDesiredWorkTime + " minutes");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    restBar.setMax(MAX_REST - MIN_REST);
    restBar.setProgress(restFromDatabase - MIN_REST);

    restBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            mDesiredRestTime = MIN_REST + progress;
            restText.setText("Current: " + mDesiredRestTime + " minutes");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

}



}
}

将此位置放在if else块之前,现在这些变量将始终保持值

0 个答案:

没有答案