共享首选项出现问题

时间:2017-10-08 01:32:05

标签: android button calendar sharedpreferences

我正在尝试使用sharedPreferences保存输入editText的内容,并显示该人在TextView中写入的数字越大,其他TextView中的数字越小,最后显示该人所提供的所有数字的数字将结果除以他们写的数字(我不是英文,我认为这个名字是平均的?,但你明白了......)。按钮代码有问题,我找不到什么,我问我的教授,他说我放错了提交,我想我已经修好了,但仍然没有用。到目前为止,这是我的代码:

public class MainActivity extends AppCompatActivity {

SharedPreferences sh;
float niligo,xukitana,garbodoro,guzzlorodo;
TextView pesbig;
TextView pessmall;
TextView pesmedio;
EditText peso;
int xa,xu,xo;
int maior,menor,media,total,quantidade;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    peso = (EditText)findViewById(R.id.editText);
    pesbig = (TextView)findViewById(R.id.View1);
    pessmall = (TextView)findViewById(R.id.View2);
    pesmedio = (TextView)findViewById(R.id.View3);

    Calendar calendar = Calendar.getInstance();
    final int day = calendar.get(Calendar.DAY_OF_YEAR); //this is me trying to lock the edit text so the person can't give more than one number a day.

    Button save = (Button) findViewById(R.id.button);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = 0;
            i++;
            SharedPreferences prefs = getSharedPreferences("prefs",Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
            xa = Integer.parseInt(peso.getText().toString());
            ed.putInt("pes", xa);
            xu = prefs.getInt("pes",xa);
            quantidade = i;
            total = xa+xu;
            media = total/quantidade;


            if (xu > xa){
                ed.putInt("pesmai", xa);
                niligo = prefs.getFloat("pesmai",xa);
                pesbig.setText(prefs.getString("pesmai", null));

            }
            else if (xu < xa){
                ed.putInt("pesmen", xa);
                xukitana = prefs.getFloat("pesmen",xa);
                pessmall.setText(prefs.getString("pesmen", null));
            }

                pesmedio.setText(String.valueOf(media));
                ed.commit();

错误日志

当EditText为空时:

10-17 20:09:22.816 10133-10133/com.omfgdogs.omfgpeso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.omfgdogs.omfgpeso, PID: 10133
                                                                   java.lang.NumberFormatException: Invalid int: ""
                                                                       at java.lang.Integer.invalidInt(Integer.java:137)
                                                                       at java.lang.Integer.parseInt(Integer.java:358)
                                                                       at java.lang.Integer.parseInt(Integer.java:331)
                                                                       at com.omfgdogs.omfgpeso.MainActivity$1.onClick(MainActivity.java:47)
                                                                       at android.view.View.performClick(View.java:4496)
                                                                       at android.view.View$PerformClick.run(View.java:18603)
                                                                       at android.os.Handler.handleCallback(Handler.java:733)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5426)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                       at dalvik.system.NativeStart.main(Native Method)

当EditText有一个数字时:

10-17 20:12:15.056 12673-12673/com.omfgdogs.omfgpeso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.omfgdogs.omfgpeso, PID: 12673
                                                                   java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float
                                                                       at android.app.SharedPreferencesImpl.getFloat(SharedPreferencesImpl.java:254)
                                                                       at com.omfgdogs.omfgpeso.MainActivity$1.onClick(MainActivity.java:63)
                                                                       at android.view.View.performClick(View.java:4496)
                                                                       at android.view.View$PerformClick.run(View.java:18603)
                                                                       at android.os.Handler.handleCallback(Handler.java:733)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5426)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                       at dalvik.system.NativeStart.main(Native Method)

〜这些线是我在这里引用的〜

0 个答案:

没有答案