有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java空共享首选项导致我的应用程序崩溃

我正在开发我的第一个应用程序,我的共享首选项有问题。 我正在尝试构建的应用程序是一个财务规划器,它可以插入每月费用和收入,以检查月末剩下多少钱

当我的共享首选项有一个值时,即通过用注释掉的麻烦代码启动应用程序并保存插入的值,应用程序可以完美工作

然而,一旦我删除输入或从头开始启动应用程序而没有保存任何内容,它就会停止工作

// 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(",", "."));

共 (0) 个答案