I'm trying to access SharedPreference from a service and getting an error which I cannot find anywhere on the net. Any help is appreciated. Thank you.
code
sharedPref = getApplicationContext().getSharedPreferences("FlutterSharedPreferences",Context.MODE_PRIVATE|Context.MODE_MULTI_PROCESS);
2020-09-10 21:16:04.057 10186-10186/com.example.demo1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo1, PID: 10186
java.lang.RuntimeException: Unable to start service com.example.demo1.MorningReset#ce7eae1 with Intent { flg=0x4 cmp=com.example.demo1/.MorningReset (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.UserManager.isUserUnlockingOrUnlocked(int)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3686)
at android.app.ActivityThread.access$1600(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.UserManager.isUserUnlockingOrUnlocked(int)' on a null object reference
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:418)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:404)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
at com.example.demo1.MorningReset.onStartCommand(MorningReset.java:25)
This error appears only when accessing the SharedPreference from a service. The same line of code works fine in an activity.
Related
Crashlytics StackTrace :
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{MerchantProfileDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3763)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3931)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2350)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:264)
at android.app.ActivityThread.main(ActivityThread.java:8312)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
I am unable to reproduce this. and it is reported only in Oppo devices.
Where should i look for clues?
I tried to run my APP, I just ended up getting a message "MegaVidi Stopped Working", I tried to run some tests and got the following errors
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.megatechnologyteam.megavidi/com.megatechnologyteam.megavidi.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
FATAL EXCEPTION: main
Process: com.megatechnologyteam.megavidi, PID: 10886
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.megatechnologyteam.megavidi/com.megatechnologyteam.megavidi.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at com.megatechnologyteam.megavidi.SplashActivity.onCreate(SplashActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
How do I solve this so that my APP can run properly?
Help me please
Please locate the object in your code
com.megatechnologyteam.megavidi.SplashActivity.onCreate(SplashActivity.java:20)
There should have an object of the class ActionBar attempting calling hide().
This just object is actually null. Please check.
In SplashActivity you are trying to hide the ActionBar which is null (that is why you are getting NullPointerException).
To resolve the issue, remove the line where you are calling ActionBar.hide().
I cannot tell you more than this without seeing the code of SplashActivity.
This error seems to be happening only on Samsung phones. I looked on the Internet and I didn't find a practical solution
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Editor$InsertionPointCursorController.hide()' on a null object reference
at android.widget.Editor.stopTextActionMode(Editor.java:2687)
at android.widget.Editor.prepareCursorControllers(Editor.java:822)
at android.widget.TextView.nullLayouts(TextView.java:9157)
at android.widget.TextView.checkForRelayout(TextView.java:10105)
at android.widget.TextView.setHintInternal(TextView.java:6538)
at android.widget.TextView.setHint(TextView.java:6527)
at com.google.android.material.textfield.TextInputLayout.dispatchProvideAutofillStructure(TextInputLayout.java:5)
at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3774)
at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3774)
at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3774)
at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3774)
at android.app.assist.AssistStructure$WindowNode.<init>(AssistStructure.java:517)
at android.app.assist.AssistStructure.<init>(AssistStructure.java:2047)
at android.app.ActivityThread.handleRequestAssistContextExtras(ActivityThread.java:3387)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7073)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)
Thank you for your help
I want to use Volley to pull some JSON data from a Service class, but I keep receiving File Cache crashes at RequestQueue mRequestQueue = Volley.newRequestQueue(this); in the service class.
It works fine in my main application.
Edit
FATAL EXCEPTION: main
Process: com.xxx.yyy, PID: 28290
java.lang.RuntimeException: Unable to instantiate service
com.idlctech.etherpooler.PoolerService:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context
android.content.Context.getApplicationContext()' on a null object
reference
at
android.app.ActivityThread.handleCreateService(ActivityThread.java:3121)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1530)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.content.Context
android.content.Context.getApplicationContext()' on a null object
reference
at
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
at
xxx.yyy.zzz.PoolerService.(PoolerService.java:38)
at java.lang.Class.newInstance(Native Method)
at
android.app.ActivityThread.handleCreateService(ActivityThread.java:3118)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1530)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
Define the service in the manifest.
<application ... >
<service android:name=".ExampleService" />
...
</application>
Look at this post. With the code you have provided us that's all I can think of.
I wasn't passing the correct context.
I am receiving these crash reports from my crash reporting tools.
I don't which code to provide here so just let me know if you want to see any particular code.
I haven't put anything related to GoogleAnalytics in onStart method as it is not mentioned in documentation.
Fatal Exception: java.lang.RuntimeException Unable to start service
com.google.android.gms.analytics.AnalyticsService#3399b37 with null:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Intent.getAction()' on a null object
reference
java.lang.RuntimeException: Unable to start service
com.google.android.gms.analytics.AnalyticsService#3399b37 with null:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Intent.getAction()' on a null object
reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2910)
at android.app.ActivityThread.access$2200(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String android.content.Intent.getAction()' on a null
object reference
at com.google.android.gms.analytics.AnalyticsService.onStartCommand()
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2893)
at android.app.ActivityThread.access$2200(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
This is a reported bug with the Google Analytics: https://code.google.com/p/analytics-issues/issues/detail?id=667.