Java Can';无法恢复价值,为什么?

Java Can';无法恢复价值,为什么?,java,android,Java,Android,这是我的应用程序(基本),通过对话框的EditText设置TextView的值 我想在应用程序启动的第一次将TextView设置为0,然后我想保存该值的状态,这样当您重新打开它时,相同的值就会出现 我将给您留下我的代码片段: public class MainActivity extends AppCompatActivity { private int currentMoney; @Override public void onCreate(Bundle savedInstanceSta

这是我的应用程序(基本),通过对话框的
EditText
设置
TextView
的值

我想在应用程序启动的第一次将
TextView
设置为0,然后我想保存该值的状态,这样当您重新打开它时,相同的值就会出现

我将给您留下我的代码片段:

public class MainActivity extends AppCompatActivity {
private int currentMoney;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    SharedPreferences mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    int current = savedInstanceState.getInt("current");
    if (mDefaultPreferences.getBoolean("first_launch", true))
    {
        mDefaultPreferences.edit().putBoolean("first_launch", false).commit();
        //Put the code here of your first launch
        currentMoney = 0;
    }
    else
    {
        //Not the first launch
        currentMoney = current;
    }

}
我会把它们分开,让它们更清楚

public void plus(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(R.layout.pluslayout);
    builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
            TextView money = (TextView) findViewById(R.id.textCool);
            EditText transaction = (EditText) ((AlertDialog) dialog).findViewById(R.id.editText);
            int earn = Integer.parseInt(transaction.getText().toString());
            int finalEarn = currentMoney + earn;
            String earnFinal = String.valueOf(finalEarn);
            money.setText(earnFinal);
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });
    AlertDialog dialog = builder.show();
}

public void minus(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(R.layout.minuslayout);
    builder.setPositiveButton(R.string.remove, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
            TextView money = (TextView) findViewById(R.id.textCool);
            EditText transaction = (EditText) ((AlertDialog) dialog).findViewById(R.id.editText);
            int loss = Integer.parseInt(transaction.getText().toString());
            int currentMoney = Integer.parseInt(money.getText().toString());
            int finalLoss = currentMoney - loss;
            String lossFinal = String.valueOf(finalLoss);
            money.setText(lossFinal);
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });
    AlertDialog dialog = builder.show();
}
最后

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    TextView money = (TextView) findViewById(R.id.textCool);
    int currentMoney = Integer.parseInt(money.getText().toString());
    savedInstanceState.putInt("current", currentMoney);
}
}


我只给您留下我的
MainActivity
Java文件,因为这可能就是问题所在。谢谢。

在文档中说builder.create();应在链接参数后显示。我认为builder.show()只会立即显示值,不会存储它

我建议使用您的SharedReference来存储和检索TextView的值。您可能已经熟悉了这个过程,因为您从SharedReferences对象中检索了一个值,但为了以防万一,这里有一个来自我的一个应用程序的片段

SharedPreferences.Editor editor = mDefaultPreferences.edit();
editor.putString("last known value", money.getText();
editor.apply();  //this will immediately write your changes in-memory, then start an asynchronous task to write the values to permanent storage on the disc.
为了把价值拿回来

String storedValue = mDefaultPreferences.getString("last known value", 0);
money.setText(storedValue);

那么我应该编写builder.create()而不是.show()?是的,请继续测试它,并告诉我它是否有效DNope,对话框也不会弹出:/@bryanherreraaw lame。logcat在说什么?等一下。。要检查请将logcat所说的添加到您的问题中立即清除:logcat在插入新方法(onRestore)后,谈到了onCreate方法中插入的saveInstanceState,现在它没有显示任何关于errorsWait的内容,我应该删除什么@Chamatake sanI的意思是,请记住,我需要在应用程序第一次启动时将值设置为0。我如何使用mDefaultPreferences??谢谢@Chamatake sanI从您的原始代码中获取了mDefaultPreferences。基本上,只要您需要保存该
TextView
的当前值,您就会使用我发布的第一段代码。例如
onSaveInstanceState()
onPause()
onStop()
。每当您需要将值重新加载到
文本视图中时,您将使用我发布的第二段代码。例如在
onStart()
onrestoreinnstancestate()
期间。要记住的关键是,当您将数据移入和移出SharedReference时,必须使用相同的
字符串
标签。是的,但它无法识别符号mPrefere。。。当我在saveInsta..()方法上粘贴代码时,为什么@查马塔克山