简体   繁体   中英

sharedpreferences not storing data in android

sharedpreferences does not store data. Error shows in getSharedPreferences this method. Error is in DetailPref key. And catlog error is : Unable to start activity ComponentInf com.example.add_fetch_data.MainActivity java.lang.NullPointerException. please any one help me what to do to store data Because I am new in Android.

public class MainActivity extends Activity {    
    Button addData, viewData, saveData, fetchData;
    EditText editName, editAdd;
    TextView textName, textAdd;
    Dialog AddDialog, ViewDialog;
    SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getSharedPreferences("DetailPref", MODE_PRIVATE);

        addData = (Button) findViewById(R.id.btn_add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            String name, address;
            @Override
            public void onClick(View arg0) {                
                AddDialog = new Dialog(MainActivity.this);
                AddDialog.setContentView(R.layout.add_fragment);
                AddDialog.setTitle("Enter Details");                
                editName = (EditText) findViewById(R.id.ed_ad_name);
                editAdd = (EditText) findViewById(R.id.ed_ad_add);
                saveData = (Button) findViewById(R.id.save_data);

                saveData.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        name = editName.getText().toString();
                        address = editAdd.getText().toString();

                        SharedPreferences.Editor edit = pref.edit();
                        // Storing data using SharedPreferences
                        edit.putString("Name", name);
                        edit.putString("Address", address);
                        edit.commit();
                        AddDialog.dismiss();                        
                    }
                }); 
                AddDialog.show();
            }
        }); 
   }
}

尝试使用edit.apply()而不是commit()。

Please clear your sharedPreferences first to test. You can do this with code or in application settings

SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.commit();

And, you are using 3 different tags. "Name", "Address" and "DetailPref": FIRST OPTION You need to save all in DetailPref.

Maybe you need a structure for this. Something like this

{"Name": "xxxxx" , address: "home"}

So you can do this:

    @Override
    public void onClick(View arg0) {
        name = editName.getText().toString();
        address = editAdd.getText().toString();

        SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPref.edit();

        JSONObject jsonToSave = new JSONObject();
        jsonToSave.put("Name", name);
        jsonToSave.put("Address", address);

        // Storing data using SharedPreferences
        edit.putString("DetailPref", jsonToSave.toString());

        edit.commit();
        AddDialog.dismiss();
    }

SECOND OPTION

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prefName = getSharedPreferences("Name", MODE_PRIVATE);
        prefAddress = getSharedPreferences("Address", MODE_PRIVATE);

        addData = (Button) findViewById(R.id.btn_add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            String name, address;
            @Override
            public void onClick(View arg0) {                
                AddDialog = new Dialog(MainActivity.this);
                AddDialog.setContentView(R.layout.add_fragment);
                AddDialog.setTitle("Enter Details");                
                editName = (EditText) findViewById(R.id.ed_ad_name);
                editAdd = (EditText) findViewById(R.id.ed_ad_add);
                saveData = (Button) findViewById(R.id.save_data);

                saveData.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        name = editName.getText().toString();
                        address = editAdd.getText().toString();

                        SharedPrefearences.Editor editName = prefName.edit();
                        SharedPreferences.Editor editAddress = prefAddress.edit();
                        // Storing data using SharedPreferences
                        editName.putString("Name", name);
                        editAddress.putString("Address", address);
                        editName.commit();
                        editAddress.commit();
                        AddDialog.dismiss();                        
                    }
                }); 
                AddDialog.show();
            }
        }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM