Java Question android.content.Context.getPackageName() problem

DonManfred

Expert
Licensed User
Longtime User
I am wrapping a Lib which used a Service. It crashes when i try to connect in the Lib.
In the connect the lib starts the Service.
Inside the Service there is a Method which uses getBaseContext() but it crashes at this line in the Service:

B4X:
    private void LoadPreferences(Boolean NotifyOfChanges) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
The
B4X:
getBaseContext()
seems to be from the Android Activity or so. don´t know exactly.
maybe android.content.ContextWrapper

Logger verbunden mit: 9885e6514556383552
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
LogMessage: Starting Service
main_btnconnect_click (java line: 365)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5359)
at com.ntrip.NTrip.doBindService(NTrip.java:276)
at com.ntrip.NTrip.Connect(NTrip.java:244)
at de.donmanfred.NTripwrapper.Connect(NTripwrapper.java:95)
at b4a.example.main._btnconnect_click(main.java:365)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
LogMessage: Starting Service
main_btnconnect_click (java line: 365)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5359)
at com.ntrip.NTrip.doBindService(NTrip.java:276)
at com.ntrip.NTrip.Connect(NTrip.java:244)
at de.donmanfred.NTripwrapper.Connect(NTripwrapper.java:95)
at b4a.example.main._btnconnect_click(main.java:365)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

The Source of the Service is attached.

Any hints how i can change the Service to get the right reference to the b4a Activity?
 

Attachments

  • ntrip.zip
    15.6 KB · Views: 439

DonManfred

Expert
Licensed User
Longtime User
I tried this but i ran into the same error.

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
NTrip_Connected(true)
AStreams_Terminated
SizeOfSourceTable: 2
---
CAS: (MyMap) {}
NET: (MyMap) {}
STR: (MyMap) {}
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
LogMessage: Starting Service
main_btnconnect_click (java line: 525)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5359)
at com.ntrip.NTrip.doBindService(NTrip.java:276)
at com.ntrip.NTrip.Connect(NTrip.java:244)
at de.donmanfred.NTripwrapper.Connect(NTripwrapper.java:95)
at b4a.example.main._btnconnect_click(main.java:525)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

My fear is that this Service depends on an old android version (maybe). The source i found is from 2012.
Or maybe the Android Activity was different in the past.
 
Top