algorhythm algorhythm - 1 year ago 848
Android Question

No such instance field

I'm trying to get my application to save some data when the orientation of the screen is changed using the

to save a boolean value

I've set numerous break points and am getting an error for the
boolean value in the variables view

mCheated= No such instance field: 'mCheated'

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?


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 = "";
public static final String EXTRA_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);

protected void onCreate(Bundle savedInstanceState) {

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);

if (savedInstanceState != null){
mCheated = savedInstanceState.getBoolean(KEY_INDEX, mCheated);

mAnswerTextView = (TextView)findViewById(;
mShowAnswer = (Button)findViewById(;
mShowAnswer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mAnswerIsTrue) {
else {
mCheated = true;


public void onSaveInstanceState(Bundle savedInstanceState){
//Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putBoolean(KEY_INDEX, mCheated);

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download