使用SharedPreference的首选项屏幕活动Android

时间:2014-08-29 13:14:00

标签: java android sharedpreferences android-preferences preferenceactivity

我试图在偏好设置屏幕中显示用户的姓名,因此他将登录并转到首选项,他将在那里看到他的名字。我对Android开发很陌生,我当然错过了一些东西,但我不知道如何解决这个问题。

我有一个MainActivity来创建一个部分:

                    SecurePreferences mSessao = new SecurePreferences(getApplicationContext(), "sessao");

                    mSessao.put("id_credencial", resultado.optString("id_credencial"));
                    mSessao.put("nome", resultado.optString("nome"));
                    mSessao.put("empresa", resultado.optString("empresa"));
                    mSessao.put("email", resultado.optString("email"));
                    mSessao.put("senha", mFerramentas.md5(mSenha));
                    mSessao.put("token", resultado.optString("token"));

PreferenceActivity用于显示登录用户的名称:

 public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);

            SecurePreferences mSessao = new SecurePreferences(getApplicationContext(), "sessao");


            Preference txtUsuario = (Preference) findPreference(getString(R.string.pref_user_profile));
            txtUsuario.setTitle(mSessao.getString("nome"));

        }

我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.com.MyPreferenceActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at br.com.MyPreferenceActivity$MyPreferenceFragment.onCreate(MyPreferenceActivity.java:42)

第42行:txtUsuario.setTitle(mSessao.getString(" nome"));

0 个答案:

没有答案