使用SharedPreferences中的现有数据填充EditText

时间:2011-01-19 01:29:57

标签: android android-edittext settext

我确信这是我忽略的基本内容,但与我研究过的所有文章相比,我似乎做得对。

我有一个DialogPreference,它包含用户名和密码的edittexts以及用于将数据保存到首选项的按钮。创建后,我想查询首选项并使用以前保存的要编辑的数据填充edittext框,否则将框保留为空白。目前,如果不存在以前的数据,我没有问题,但如果数据确实存在,我的应用程序在尝试打开DialogPreference时崩溃。

我的DialogPreference代码:

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);
        }
    }


}

1 个答案:

答案 0 :(得分:2)

这是因为你获得了NullPointerException。您在创建edittex之前调用existingData()。它应该以这种方式工作:

    // 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);

最后一条建议:学习如何使用logcat工具。它会告诉您应用崩溃的原因,时间和地点。