title: Filling EditText with existing data from SharedPreferences
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>My DialogPreference code:</p>
  
  <pre><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) &amp;&amp; (Pworddata.length() != 0)) {
              mUserbox.setText(Unamedata);
              mPassbox.setText(Pworddata);
          }
      }
  
  
  }
  </code></pre>