所以我想+两个双打,但它给出了一些非常奇怪的结果。 在这里你可以看到它的外观Here认为它与加上字符串和没有putDouble的SharedPreferences有关。我已经尝试了最后2个小时来解决它,但我无法让它工作,有人可以帮助我。 这是代码:
package me.danilkp1234.laerkeholtmilk;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import com.google.firebase.crash.FirebaseCrash;
import static me.danilkp1234.laerkeholtmilk.R.id.brix;
import static me.danilkp1234.laerkeholtmilk.R.id.textView;
import static me.danilkp1234.laerkeholtmilk.R.id.textView3;
public class MainActivity extends AppCompatActivity {
private EditText milkdone;
private EditText brixdone;
private double milkdone2;
private double brixdone2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
milkdone = (EditText) findViewById(R.id.milkliter);
brixdone = (EditText) findViewById(R.id.brix);
SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
String MilkDone2Saved=(prefs.getString("MilkDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(MilkDone2Saved)){
milkdone2 = Double.parseDouble(MilkDone2Saved);
}
String BrixDone2Saved=(prefs.getString("BrixDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(BrixDone2Saved)){
brixdone2 = Double.parseDouble(BrixDone2Saved);
}
brixdone.setText(String.valueOf(brixdone2));
milkdone.setText(String.valueOf(milkdone2));
milkdone.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
String MilkDone2Saved=(prefs.getString("MilkDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(MilkDone2Saved)){
milkdone2 = Double.parseDouble(MilkDone2Saved);
}
String BrixDone2Saved=(prefs.getString("BrixDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(BrixDone2Saved)){
brixdone2 = Double.parseDouble(BrixDone2Saved);
}
double test2 = brixdone2 + milkdone2;
TextView change = (TextView) findViewById(R.id.textView3);
change.setText(String.valueOf(test2));
handled = true;
}
return handled;
}
});
brixdone.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE){
SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
String MilkDone2Saved=(prefs.getString("MilkDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(MilkDone2Saved)){
milkdone2 = Double.parseDouble(MilkDone2Saved);
}
String BrixDone2Saved=(prefs.getString("BrixDone2Saved",String.valueOf(milkdone)));
if(!TextUtils.isEmpty(BrixDone2Saved)){
brixdone2 = Double.parseDouble(BrixDone2Saved);
}
double test2 = brixdone2 + milkdone2;
TextView change = (TextView) findViewById(R.id.textView3);
change.setText(String.valueOf(test2));
handled = true;
}
return handled;
}
});
};
public void onStop() {
super.onStop();
storeDouble("MilkDone2Saved",milkdone);
storeDouble("BrixDone2Saved",brixdone);
}
public void storeDouble(String d, EditText c){
SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(d,String.valueOf(c.getText().toString()));
editor.apply();
}
public void GetDouble(){
SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
}
}
它从double转换为string加倍(我认为)。 它应该做brixdone2 + milkdone2并且它不是正确的结果 编辑:更改它,现在应用程序崩溃
Caused by java.lang.ClassCastException
java.lang.String cannot be cast to java.lang.Long
android.app.SharedPreferencesImpl.getLong (SharedPreferencesImpl.java:279)
me.danilkp1234.laerkeholtmilk.MainActivity.getDouble (MainActivity.java:31)
me.danilkp1234.laerkeholtmilk.MainActivity.onCreate (MainActivity.java:40)
android.app.Activity.performCreate (Activity.java:7174)
答案 0 :(得分:0)
String MilkDone2Saved =(prefs.getString(“MilkDone2Saved”,String.valueOf(milkdone)));
getString返回与 MikeDone2Saved 键关联的字符串,如果找不到键,则默认值为
将String.valueOf(milkdone)
但我知道 onCreate()中的 String.valueOf(milkdone)会有垃圾值,所以请再次检查一下......