Search code examples
javaandroidandroid-studiosharedpreferencesandroid-studio-4.2

How to use a String stored in sharedPreference?


I have this two methods "saveCurrentPlayer" and "loadCurrentPlayer" in a class "PlayersActivity" to save and load a String.

public void saveCurrentPlayer(){
        SharedPreferences preferences = getSharedPreferences("currentPlayer", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("currentPlayer",jugadorEliminar);
        editor.apply();
    }

    public String loadCurrentPlayer(){
        SharedPreferences preferences = getSharedPreferences("currentPlayer",Context.MODE_PRIVATE);
        String currentPlayer = preferences.getString("currentPlayer","");
        return currentPlayer;
    }

I try to get that String "currentPlayer" from another class "QuestionAnswerManagerActivity" but I get force close.

Toast.makeText(QuestionAnswerManagerActivity.this,playersActivity.loadCurrentPlayer(),Toast.LENGTH_SHORT).show();

Here the logcat:

2021-06-29 19:00:25.584 12936-12936/? I/actual.pregres: type=1400 audit(0.0:9796): avc: denied { sendto } for path="/dev/socket/logdw" scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_dgram_socket permissive=1
2021-06-29 19:00:25.668 12936-12936/? I/zygote: Late-enabling -Xcheck:jni
2021-06-29 19:00:28.285 12936-12936/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-06-29 19:00:28.696 12936-12936/? I/JDWP: type=1400 audit(0.0:9801): avc: denied { connectto } for path=006A6477702D636F6E74726F6C scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2021-06-29 19:00:28.840 12936-12936/? I/JDWP: type=1400 audit(0.0:9802): avc: denied { read write } for path="socket:[145879]" dev="sockfs" ino=145879 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2021-06-29 19:00:31.308 12936-12936/? I/re-initialized>: type=1400 audit(0.0:9807): avc: denied { write } for path="socket:[145879]" dev="sockfs" ino=145879 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2021-06-29 19:00:34.828 12936-12936/com.doctoractual.pregresp I/com.doctoractual.pregresp: type=1400 audit(0.0:9828): avc: denied { write } for comm=45474C20496E6974 name="property_service" dev="tmpfs" ino=174 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=1
2021-06-29 19:00:34.844 12936-12936/com.doctoractual.pregresp I/com.doctoractual.pregresp: type=1400 audit(0.0:9830): avc: denied { connectto } for comm=45474C20496E6974 path="/dev/socket/property_service" scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2021-06-29 19:00:35.071 12936-12962/com.doctoractual.pregresp I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/egl/libGLES_emulation.so from the current namespace instead.
2021-06-29 19:00:35.231 12936-12962/com.doctoractual.pregresp I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/egl/libEGL_emulation.so from the current namespace instead.
2021-06-29 19:00:35.639 12936-12962/com.doctoractual.pregresp I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/egl/libGLESv1_CM_emulation.so from the current namespace instead.
2021-06-29 19:00:35.672 12936-12962/com.doctoractual.pregresp I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/egl/libGLESv2_emulation.so from the current namespace instead.
2021-06-29 19:00:36.080 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9835): avc: denied { write } for name="BASE_DATOS" dev="sdb3" ino=81843 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0:c512,c768 tclass=file permissive=1
2021-06-29 19:00:36.080 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9836): avc: denied { open } for path="/data/data/com.doctoractual.pregresp/databases/BASE_DATOS" dev="sdb3" ino=81843 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0:c512,c768 tclass=file permissive=1
2021-06-29 19:00:36.120 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9837): avc: denied { lock } for path="/data/data/com.doctoractual.pregresp/databases/BASE_DATOS" dev="sdb3" ino=81843 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0:c512,c768 tclass=file permissive=1
2021-06-29 19:00:36.173 12936-12970/com.doctoractual.pregresp D/OpenGLRenderer: HWUI GL Pipeline
2021-06-29 19:00:36.222 12936-12947/com.doctoractual.pregresp I/zygote: Background concurrent copying GC freed 4602(981KB) AllocSpace objects, 0(0B) LOS objects, 66% free, 780KB/2MB, paused 8.318ms total 54.936ms
2021-06-29 19:00:36.492 12936-12936/com.doctoractual.pregresp I/RenderThread: type=1400 audit(0.0:9844): avc: denied { write } for name="local_opengl" dev="tmpfs" ino=14777 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:socket_device:s0 tclass=sock_file permissive=1
2021-06-29 19:00:36.643 12936-12970/com.doctoractual.pregresp I/OpenGLRenderer: Initialized EGL, version 1.4
2021-06-29 19:00:36.643 12936-12970/com.doctoractual.pregresp D/OpenGLRenderer: Swap behavior 1
2021-06-29 19:00:36.647 12936-12970/com.doctoractual.pregresp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-06-29 19:00:36.647 12936-12970/com.doctoractual.pregresp D/OpenGLRenderer: Swap behavior 0
2021-06-29 19:00:36.677 12936-12970/com.doctoractual.pregresp D/EGL_emulation: eglCreateContext: 0xe7a051e0: maj 3 min 1 rcv 4
2021-06-29 19:00:36.760 12936-12970/com.doctoractual.pregresp I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/hw/gralloc.vbox86.so from the current namespace instead.
2021-06-29 19:00:36.778 12936-12970/com.doctoractual.pregresp E/eglCodecCommon: goldfish_dma_create_region: could not obtain fd to device! fd -1 errno=2
2021-06-29 19:00:37.177 12936-12936/com.doctoractual.pregresp I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.
2021-06-29 19:00:38.704 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9870): avc: denied { open } for path="/dev/__properties__/u:object_r:serialno_prop:s0" dev="tmpfs" ino=136 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serialno_prop:s0 tclass=file permissive=1
2021-06-29 19:00:38.704 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9871): avc: denied { getattr } for path="/dev/__properties__/u:object_r:serialno_prop:s0" dev="tmpfs" ino=136 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serialno_prop:s0 tclass=file permissive=1
2021-06-29 19:00:41.640 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/os/PowerManager
2021-06-29 19:00:41.642 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/IntentService
2021-06-29 19:00:41.643 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/os/PowerManager$WakeLock
2021-06-29 19:00:41.649 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/Instrumentation
2021-06-29 19:00:41.650 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/JobSchedulerImpl
2021-06-29 19:00:41.651 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/job/JobServiceEngine$JobHandler
2021-06-29 19:00:41.663 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/ActivityThread
2021-06-29 19:00:41.666 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/os/Debug
2021-06-29 19:00:41.667 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/AlarmManager
2021-06-29 19:00:41.668 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/job/JobService
2021-06-29 19:00:41.669 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/PendingIntent
2021-06-29 19:00:41.673 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/location/LocationManager
2021-06-29 19:00:41.675 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: java/net/URL
2021-06-29 19:00:41.684 12936-12936/com.doctoractual.pregresp W/com.doctoractual.pregresp: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
2021-06-29 19:00:41.687 12936-12936/com.doctoractual.pregresp I/chatty: uid=10071(u0_a71) com.doctoractual.pregresp identical 11 lines
2021-06-29 19:00:41.688 12936-12936/com.doctoractual.pregresp W/com.doctoractual.pregresp: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
2021-06-29 19:00:42.597 12936-12936/com.doctoractual.pregresp W/zygote: Verification of void android.app.ActivityThread.handleStopBinderTrackingAndDump(android.os.ParcelFileDescriptor) took 108.552ms
2021-06-29 19:00:42.911 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/AlarmManager$ListenerWrapper
2021-06-29 19:00:42.912 12936-12936/com.doctoractual.pregresp W/com.doctoractual.pregresp: Changing class Landroid/app/AlarmManager$ListenerWrapper;
2021-06-29 19:00:42.914 12936-12936/com.doctoractual.pregresp W/com.doctoractual.pregresp: Dex file created by class-definition time transformation of Landroid/app/AlarmManager$ListenerWrapper; is not checked for all retransformation invariants.
2021-06-29 19:00:43.072 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/app/AlarmManager$ListenerWrapper
2021-06-29 19:00:43.074 12936-12936/com.doctoractual.pregresp W/zygote: Verification of void android.app.AlarmManager.setImpl(int, long, long, long, int, android.app.PendingIntent, android.app.AlarmManager$OnAlarmListener, java.lang.String, android.os.Handler, android.os.WorkSource, android.app.AlarmManager$AlarmClockInfo) took 321.983ms
2021-06-29 19:00:43.118 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Transformed class: android/location/LocationManager$ListenerTransport
2021-06-29 19:00:43.119 12936-12936/com.doctoractual.pregresp W/com.doctoractual.pregresp: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
2021-06-29 19:00:43.202 12936-12936/com.doctoractual.pregresp W/zygote: Verification of void android.location.LocationManager.requestLocationUpdates(android.location.LocationRequest, android.location.LocationListener, android.os.Looper, android.app.PendingIntent) took 123.589ms
2021-06-29 19:00:43.234 12936-12936/com.doctoractual.pregresp V/StudioProfiler: Profiler initialization complete on agent.
2021-06-29 19:00:43.250 12936-13005/com.doctoractual.pregresp V/StudioProfiler: Acquiring Application for Events
2021-06-29 19:00:43.535 12936-12941/com.doctoractual.pregresp I/zygote: Do partial code cache collection, code=29KB, data=26KB
2021-06-29 19:00:43.536 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=29KB, data=26KB
2021-06-29 19:00:43.536 12936-12941/com.doctoractual.pregresp I/zygote: Increasing code cache capacity to 128KB
2021-06-29 19:00:45.193 12936-12941/com.doctoractual.pregresp I/zygote: Do partial code cache collection, code=45KB, data=44KB
2021-06-29 19:00:45.195 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=45KB, data=44KB
2021-06-29 19:00:45.205 12936-12941/com.doctoractual.pregresp I/zygote: Increasing code cache capacity to 256KB
2021-06-29 19:00:45.238 12936-12941/com.doctoractual.pregresp I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-06-29 19:00:45.238 12936-12941/com.doctoractual.pregresp I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-06-29 19:00:45.582 12936-12941/com.doctoractual.pregresp I/zygote: Do full code cache collection, code=124KB, data=66KB
2021-06-29 19:00:45.588 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=107KB, data=42KB
2021-06-29 19:00:50.239 12936-12941/com.doctoractual.pregresp I/zygote: Do partial code cache collection, code=124KB, data=76KB
2021-06-29 19:00:50.236 12936-12936/com.doctoractual.pregresp I/com.doctoractual.pregresp: type=1400 audit(0.0:9879): avc: denied { sendto } for comm=4A69742074687265616420706F6F6C path="/dev/socket/logdw" scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_dgram_socket permissive=1
2021-06-29 19:00:50.250 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=124KB, data=76KB
2021-06-29 19:00:50.250 12936-12941/com.doctoractual.pregresp I/zygote: Increasing code cache capacity to 512KB
2021-06-29 19:00:50.340 12936-12936/com.doctoractual.pregresp I/Choreographer: Skipped 52 frames!  The application may be doing too much work on its main thread.
2021-06-29 19:00:50.733 12936-12941/com.doctoractual.pregresp I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-06-29 19:00:50.932 12936-12941/com.doctoractual.pregresp I/zygote: Do full code cache collection, code=244KB, data=143KB
2021-06-29 19:00:50.933 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=238KB, data=98KB
2021-06-29 19:00:52.121 12936-12941/com.doctoractual.pregresp I/zygote: Do partial code cache collection, code=243KB, data=108KB
2021-06-29 19:00:52.123 12936-12941/com.doctoractual.pregresp I/zygote: After code cache collection, code=243KB, data=108KB
2021-06-29 19:00:52.130 12936-12941/com.doctoractual.pregresp I/zygote: Increasing code cache capacity to 1024KB
2021-06-29 19:01:01.116 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9884): avc: denied { read } for name="databases" dev="sdb3" ino=81729 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0:c512,c768 tclass=dir permissive=1
2021-06-29 19:01:01.116 12936-12936/com.doctoractual.pregresp I/actual.pregresp: type=1400 audit(0.0:9885): avc: denied { open } for path="/data/data/com.doctoractual.pregresp/databases" dev="sdb3" ino=81729 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0:c512,c768 tclass=dir permissive=1
2021-06-29 19:01:01.142 12936-12936/com.doctoractual.pregresp D/AndroidRuntime: Shutting down VM
2021-06-29 19:01:01.144 12936-12936/com.doctoractual.pregresp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.doctoractual.pregresp, PID: 12936
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.doctoractual.pregresp.PlayersActivity.loadCurrentPlayer()' on a null object reference
        at com.doctoractual.pregresp.QuestionAnswerManagerActivity$4.onClick(QuestionAnswerManagerActivity.java:216)
        at android.view.View.performClick(View.java:6256)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:24697)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

But writing a text directly there is no problem, everything works.

Toast.makeText(QuestionAnswerManagerActivity.this,"Jugador Luis",Toast.LENGTH_SHORT).show();

Someone can see what I am doing wrong? Thanks in advance.


Solution

  • You are dependent on some other activity in order to use SharedPreferences. In this case, playersActivity is null when you have tried to use it. This is a very bad Android development design as you do not need SharedPreferences to have any dependencies, it can be fully independent.

    Solution: 3 Options

    1. Turn your SharedPreferences in to Static Methods. (ex. loadCurrentPlayer in playersActivity should be Static)

    2. Best Practice: Create a new Manager class that is responsible for app's entire SharedPreferences usage (ex. SharedPreferenceManager). This class is a Singleton Class that is instantiated with ApplicationContext and has all the methods related CRUD operations in SharedPreferences, in your case saveCurrentPlayer and loadCurrentPlayer. Since the manager class is independent, it should not cause any Lifecycle errors or NullPointer errors.

      Since attaching sample code will clutter my answer, I decided to cite someone else's code here, this will teach you how to implement it.

    3. If you are too lazy to follow #2: You can use a library that has done that for you already: Pixplicity/EasyPrefs.

      • Easy to use like Manager Class ✅
      • No more errors related to SharedPreferences
      • You could always retrofit into any existing project ✅

    Let me know if you have any questions. Thanks.