重新启动应用程序时,我的Sharedpreference无法保存

崔弘淑

我只有2个活动,分别是Main和Second活动,但是sharedpreference在2个活动之间起作用,但是当我重新启动它时,它永远不会进入if语句(Main活动)if(prefs!= null),这意味着当我重新启动它时,prefs始终为null 。关闭应用程序时,sharedpreference是否实际保存数据?提前谢谢:)

MainActivity

package com.example.hongsukchoi.rentaltruck;

import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class MainActivity extends AppCompatActivity {

    private EditText mile_edittext;
    private RadioGroup radio;
    private Button submit_button;
    double truck_price;
    double mile_price;
    double total_price;
    private int truck_type;
    private RadioButton ten;
    private RadioButton seventeen;
    private RadioButton twentysix;


    SharedPreferences prefs;
    SharedPreferences.Editor editor;

    Set<String> mySet = new HashSet<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ten = (RadioButton) findViewById(R.id.tenfeettruck);
        seventeen = (RadioButton) findViewById(R.id.seventeenfeettruck);
        twentysix = (RadioButton) findViewById(R.id.twentysixfeettruck);


        //ten.setChecked(true);

        if(prefs != null) {
            System.out.println("This is what I am looking for " + prefs.getAll());
            //R.id.checked
            ten.setChecked(true);


        }
        System.out.println("Application is now started");

        //ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
       // System.out.println("HERE ARE ALL THE PREFERENCES(Main acivity): " + allPrefs.toString());





        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prefs = getSharedPreferences("type", MODE_PRIVATE);
        editor = prefs.edit();

        radio = (RadioGroup) findViewById(R.id.truckRadio);
        submit_button = (Button) findViewById(R.id.submit_button);
        mile_edittext = (EditText) findViewById(R.id.mile_edittext);



        submit_button.setOnClickListener(new View.OnClickListener(){


            @Override
            public void onClick(View v) {

                String temp3 = mile_edittext.getText().toString();
                mile_price = Double.parseDouble(temp3);




                total_price = truck_price + (mile_price*2);


                String total2 = String.valueOf(total_price);

                //editor.putString("total", total2);
                //editor.putStringSet("total_set", mySet);
                editor.putInt("type", truck_type);
                editor.commit();



                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                i.putExtra("total",total2);
                //i.putExtra("type", truck_type);
                startActivity(i);







            }
        });








    }
    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.tenfeettruck:
                if (checked)
                    truck_price = 19.99;
                truck_type = 10;

                mySet.add(String.valueOf(truck_type));



                break;
            case R.id.seventeenfeettruck:
                if (checked)
                    truck_price = 29.99;
                truck_type = 17;


                mySet.add(String.valueOf(truck_type));


                break;
            case R.id.twentysixfeettruck:
                if (checked)
                    truck_price = 39.99;
                truck_type = 26;


                mySet.add(String.valueOf(truck_type));


                break;
        }
    }




}


SecondActivity

package com.example.hongsukchoi.rentaltruck;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class SecondActivity extends Activity {
    private double total_price;
    private ImageView myImgView;
    private TextView Total_displaying;
    private TextView whichtruck;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        myImgView = (ImageView)findViewById(R.id.truckimage);
        whichtruck = (TextView)findViewById(R.id.whichtruck);
        SharedPreferences prefs = getSharedPreferences("type", MODE_PRIVATE);

        SharedPreferences prefs_set = getSharedPreferences("total_set", MODE_PRIVATE);

        if(prefs.getStringSet("total_set", null) != null) {

            ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
            System.out.println("HERE ARE ALL THE PREFERENCES(Second acivity): " + allPrefs.toString());
        }

        //SharedPreferences type = getSharedPreferences("type", MODE_PRIVATE);
        Intent i = getIntent();
        String temp = i.getExtras().getString("total");

        int type_temp = prefs.getInt("type", 0);


        if(temp != null) {
             total_price = Double.parseDouble(temp);
        }



        if(type_temp == 10)
        {
            myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckten));
            whichtruck.setText("is 10 feet truck!");

        }
        else if(type_temp == 17)
        {
            myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckseventeen));
            whichtruck.setText("is 17 feet truck!");

        }
        else if(type_temp == 26)
        {
            myImgView.setImageDrawable(getResources().getDrawable(R.drawable.trucktwentysix));
            whichtruck.setText("is 26 feet truck!");

        }

        Total_displaying = (TextView) findViewById(R.id.Total_displaying);
        String display = Double.toString(total_price);
        Total_displaying.setText(display);
    }


}
塔米尔·阿布特布尔

当应用程序关闭时,sharedpreference会保存数据。
您正在检查“ prefs”是否不为空,然后再启动它。
请尝试下面的代码,并检查是否可以解决您的问题:

里面的onCreate:

   prefs = getSharedPreferences("type", MODE_PRIVATE);

内部onStart:

   if(prefs != null) {
        System.out.println("This is what I am looking for " + prefs.getAll());
        //R.id.checked
        ten.setChecked(true);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

重新启动应用程序时保存布尔状态

NSUserDefaults在使用应用程序时保存并重新加载,但在重新启动应用程序时重置

重新启动我的应用程序时,firebase 替换了我的数据

UIWebView:在重新启动应用程序时保存cookie状态

重新启动应用程序时,SharedPref不保存更改

返回应用程序时重新启动UIViewPropertyAnimator

重新启动应用程序时列出 NullPointerException

每次重新启动应用程序时,我都应该调用setMinimumBackgroundFetchInterval吗?

离开并返回我的应用程序时,android重新启动活动

每次我重新启动应用程序时,Flutter都会加载应用程序的旧版本

我可以获取仅在首次启动时保存的实体。如果我重新启动该应用程序,则无法获取我的实体

当用户在 Android 中关闭应用程序时如何重新启动应用程序

即使应用程序重新启动,也保存 UISwitch 状态

即使应用程序重新启动也保存图像

重新启动应用程序后保存布尔状态

重新启动应用程序后,无法在设备中找到保存的文件

重新启动应用程序后,为什么我的数据没有保存到CoreData?

Spring应用程序无法重新启动

在我在Android M上重新启动应用程序之前,无法编写外部存储设备

每当我重新启动Android应用程序时,Firebase中的投票计数都会重置

我的应用程序中的 Viewpager Activity 重新启动

将画布路径保存到SharedPreference并在重新打开应用程序时在画布上重画

重新启动应用程序时事件处理程序重播?

每次重新启动应用程序时,Spring Integration都会加载文件

为什么每次重新启动应用程序时,BluetoothDevice的地址都会更改?

重新启动应用程序时,来自Xamarin消息中心的多条消息

重新启动应用程序时,emberfire 如何维护身份验证会话?

重新启动应用程序时,GC日志循环数据丢失

重新启动Java应用程序时重用Selenium WebDriver会话