Search code examples
androidandroid-edittextsettext

Filling EditText with existing data from SharedPreferences


I'm sure this is something really basic that I'm overlooking but compared with all the articles I've researched, I seem to be doing it right.

I have a DialogPreference that has edittexts for both username and password and a button to save the data to preferences. Upon creation, I'd like to query the preferences and fill the edittext boxes with previously saved data to be edited, otherwise leave the boxes blank. Currently, if no previous data exists, I'm having no issues, but if data does exist, my app crashes when trying to open the DialogPreference.

My DialogPreference code:

public class AccDialog extends DialogPreference implements DialogInterface.OnClickListener {

    private EditText mUserbox, mPassbox;
    CharSequence mPassboxdata, mUserboxdata;
    private Context mContext;

    private int mWhichButtonClicked;


    public AccDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

    }

    @Override
    protected View onCreateDialogView() {

        // Access default SharedPreferences
        @SuppressWarnings("unused")
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);

        // Register listener
        final OnCheckedChangeListener mShowchar_listener;


        // Run the following methods onCreate
        existingData();


        @SuppressWarnings("unused")
        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setPadding(10, 10, 10, 10);
            layout.setBackgroundColor(0xFF000000);


            mUserbox = new EditText(mContext);
                mUserbox.setSingleLine(true);   
                mUserbox.setSelectAllOnFocus(true);

            mPassbox = new EditText(mContext);
                mPassbox.setSingleLine(true);
                mPassbox.setSelectAllOnFocus(true);

            layout.addView(mUserbox);
            layout.addView(mPassbox);

        return layout;  
    }   


    private void existingData() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
        String Unamedata = pref.getString("usernamekey", "");
        String Pworddata = pref.getString("passwordkey", "");

        if((Unamedata.length() != 0) && (Pworddata.length() != 0)) {
            mUserbox.setText(Unamedata);
            mPassbox.setText(Pworddata);
        }
    }


}

Solution

  • It's because you get a NullPointerException. You are calling existingData() before the edittexs are created. It should work this way:

        // initialize them first!!!!
        mUserbox = new EditText(mContext);
        mPassbox = new EditText(mContext);
        // Run the following methods onCreate
        existingData();
    
    
        @SuppressWarnings("unused")
        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setPadding(10, 10, 10, 10);
            layout.setBackgroundColor(0xFF000000);
    
                mUserbox.setSingleLine(true);   
                mUserbox.setSelectAllOnFocus(true);
    
                mPassbox.setSingleLine(true);
                mPassbox.setSelectAllOnFocus(true);
    
            layout.addView(mUserbox);
            layout.addView(mPassbox);
    

    Just a last advice: learn how to use the logcat tool. It will show you why, when and where your app crashes.