0

I'm trying to show the name of the user in a preference screen, so he will login and go to the preferences and he will see his name there. I'm very new to Android Development, I'm certainly missing something, but I don't know how to solve this.

I have a MainActivity that creates a section:

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

And a PreferenceActivity to show the name of user logged in:

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

        }

I'm getting this error:

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)

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

AND4011002849
  • 1,971
  • 8
  • 38
  • 81
  • PreferenceActivity uses the `SharedPreferences` automatically, not a database. Also, a `PreferenceActivity` is for setting preferences, not to display random stuff. I think you should just use a normal Preferences instead, also indicate line `MyPreferenceActivity.java:42)` – EpicPandaForce Aug 29 '14 at 13:20
  • Line 42: txtUsuario.setTitle(mSessao.getString("nome")); – AND4011002849 Aug 29 '14 at 13:26
  • then either `txtUsuario` or `mSessao` is *null*. Use `if(txtUsuario == null) { Log.wtf(this.getClass().getSimpleName(), "txtUsario is null"); }` – EpicPandaForce Aug 29 '14 at 13:28
  • Yes, "txtUsario is null", how can I fix that? – AND4011002849 Aug 29 '14 at 13:33
  • I guess the preference doesn't exist and returns `null` unless it's set. Although... I need to check what a `Preference` actually is. I have a guess that this is not what you are looking for, you should just have a `TextView` in a normal activity or something. – EpicPandaForce Aug 29 '14 at 14:26
  • @Zhuinden thank you for your time, how can I get the "nome" string from my sharedpreference and show in my preference activity? – AND4011002849 Aug 29 '14 at 14:28
  • http://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-android?rq=1 – EpicPandaForce Aug 29 '14 at 14:31
  • Where does `SecurePreferences`come? Are you using this [library](https://github.com/scottyab/secure-preferences/blob/master/sample/src/com/securepreferences/sample/MainActivity.java)? – Víctor Albertos Aug 29 '14 at 15:03

0 Answers0