Java 没有这样的实例字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28646999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:33:58  来源:igfitidea点击:

No such instance field

javaandroidvariablesinstance

提问by algorhythm

I'm trying to get my application to save some data when the orientation of the screen is changed using the onSaveInstanceStateto save a boolean value mCheated.

我试图让我的应用程序在使用onSaveInstanceState保存布尔值更改屏幕方向时保存一些数据mCheated

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

我设置了许多断点,并且mCheated在变量视图中收到布尔值错误

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?

我不知道为什么我在活动开始时将其声明为 false 值,并在按下按钮时将其更改为 true 。谁能帮我吗?

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);
    }
}

采纳答案by Amiit

Check if your build variant in Android Studio has

检查您在 Android Studio 中的构建变体是否具有

  • debuggableas true
  • proguardis disabled or commented out.
  • 可调试为真
  • proguard被禁用或注释掉。

回答by algorhythm

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.

结果证明代码没有问题,Android Studio 需要重新启动。我认为这是因为我克隆了该项目并且可能使用了以前版本中的错误文件。

回答by Android is everything for me

I got the same error and I wasted my 3-4 hours to resolve same error finally I got to know why that happened and it was interesting

我遇到了同样的错误,我浪费了 3-4 个小时来解决同样的错误,最后我知道为什么会发生这种情况,这很有趣

  1. In my case, I changed the code in one file (I declared one variable and initialized it)

  2. I run the apk from my device and attached debugger from android studio

  3. I set debug point to that newly added variable where I assigned data to it

  4. but during debugging it shows me same error

  5. Then I got to know I changed the code in file but I run the apk from device, and I attached debugger I need to compile and run the changes instead of it how it will reflect in apk that was the actual issue

  6. So I compiled and ran the code and installed newly compiled apk on device then I attached debugger and it worked for me

  1. 就我而言,我更改了一个文件中的代码(我声明了一个变量并对其进行了初始化)

  2. 我从我的设备运行 apk 并从 android studio 附加调试器

  3. 我将调试点设置为我为其分配数据的新添加的变量

  4. 但在调试过程中它显示了同样的错误

  5. 然后我知道我更改了文件中的代码,但我从设备运行了 apk,我附加了调试器我需要编译和运行更改而不是它如何反映在 apk 中,这是实际问题

  6. 所以我编译并运行了代码并在设备上安装了新编译的 apk 然后我附加了调试器,它对我有用

hope this will save someone's time

希望这会节省某人的时间

回答by Ali

If you're using pro-guard and obfuscation is true. you have to comment out obfuscation in build gradle

如果您使用的是 pro-guard 和混淆是真的。你必须在构建 gradle 中注释掉混淆

eg: add this in the pro-guard -dontobfuscate

例如:在亲卫队中添加这个 -dontobfuscate

回答by Akash Bisariya

I had the same error. The solution to the error is to disable the Proguard in build.gradle file.

我有同样的错误。该错误的解决方法是禁用 build.gradle 文件中的 Proguard。

 debug {
        minifyEnabled false
}

回答by isles1217

In case this comes in handy for anyone else, I was able to resolve this issue immediately once I realized I was in 'Release' mode, not 'Debug' mode, in the Build Variants section.

如果这对其他人有用,一旦我意识到我在“构建变体”部分中处于“发布”模式而不是“调试”模式,我就能够立即解决此问题。