我正在开发我的第一个应用程序,但我的共享首选项有问题。 我正在尝试构建的应用程序是一个财务计划员,用于插入每月支出和收入,以检查一个月底还剩下多少钱。
当我的共享首选项具有值时,即通过在注释掉麻烦的代码的情况下启动应用程序并保留插入的值,该应用程序将完美运行。
但是,一旦我删除输入或从头开始启动应用程序而没有保存任何内容,它就会停止工作。
// Setting up shared preferences
FinancePlanPreferences = getSharedPreferences("FinancePlan", MODE_PRIVATE);
String netIncomeAmount =
FinancePlanPreferences.getString("netIncomeAmount", "");
// Setting up EditText
netIncomeInput = (EditText) findViewById(R.id.netIncomeInput);
//here I tried to workaround the problem
if (netIncomeInput.equals("")) {
netIncomeInput.setText("0,00");
} else {netIncomeInput.setText(FinancePlanPreferences.getString("netIncomeAmount",""));
// receive the Input
String netIncomeAmount = netIncomeInput.getText().toString();
// store Input in shared preferences
SharedPreferences.Editor editor = FinancePlanPreferences.edit();
editor.putString("netIncomeAmount", netIncomeAmount);
editor.apply();
// and this line causes the problem. it works when there is a value saved. (p.s. the replace part is since the numbers are inserted in the german format)
netIncomeAmountTextView.setText(FinancePlanPreferences.getString("netIncomeAmount", "").replace(".", "").replace(",", "."));