没有这样的实例字段 [英] No such instance field
问题描述
我正在尝试让我的应用程序在使用 onSaveInstanceState
更改屏幕方向时保存一些数据以保存布尔值 mCheated
。
I'm trying to get my application to save some data when the orientation of the screen is changed using the onSaveInstanceState
to save a boolean value mCheated
.
我设置了很多断点,并且变量视图中的 mCheated
布尔值出现错误
I've set numerous break points and am getting an error for the mCheated
boolean value in the variables view
mCheated= No such instance field: 'mCheated'
我不知道为什么我在活动开始时声明值为false,如果按下按钮则将其更改为true。任何人都可以帮我吗?
I have no idea why as I declare it with a value false when the activity is started and change it to true if a button is pressed. Can anyone help me out?
package com.bignerdranch.android.geoquiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Chris on 20/02/2015.
*/
public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown";
private static final String KEY_INDEX = "index";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private boolean mCheated = false;
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
if (savedInstanceState != null){
mCheated = savedInstanceState.getBoolean(KEY_INDEX, mCheated);
}
setAnswerShownResult(mCheated);
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
}
else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
mCheated = true;
}
});
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
//Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putBoolean(KEY_INDEX, mCheated);
}
}
推荐答案
它事实证明代码没有问题,Android Studio需要重启。我认为这取决于我克隆了项目的事实,并且可能使用了以前版本中的错误文件。
It turns out there wasn't a problem with the code and that Android Studio required a restart. I think it was down to the fact I had cloned the project and was possibly using an incorrect file from the previous version.
这篇关于没有这样的实例字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!