Android Question service crash when applying rect

Shivito1

Active Member
Licensed User
when the following line is ran the error is thrown
B4X:
r.Initialize(0,-400%y, cvs.Bitmap.Width, cvs.Bitmap.Height)
maybe this can not be done in a service???

B4X:
** Service (starter) Create **
** Service (starter) Start **
Media Projection is starting
Error occurred on line: 62 (Starter)
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getHeight()' on a null object reference
    at anywheresoftware.b4a.keywords.Common.PerYToCurrent(Common.java:644)
    at b4a.example.starter._drawroundbitmap(starter.java:229)
    at b4a.example.starter._service_start(starter.java:434)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
    at b4a.example.starter.handleStart(starter.java:102)
    at b4a.example.starter.onStartCommand(starter.java:70)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3738)
    at android.app.ActivityThread.-wrap23(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6780)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
 

DonManfred

Expert
Licensed User
Longtime User
Can you use a canvas in a service?
My 1st answer was "You need an activity context for this" but then i was unsure and changed my answer.

You are probably right. The Service can call a Activity which does the calculation.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
In fact that's more an issue about the geometry because the service can't get it by itself. For example if you pass a view using a global list, get the view in the service, you can work on that view locally inside a service and update the view in the activity while in foreground (I posted something about some years ago).
So here we got an error because referring to a size which the service is unable to retrieve. If we sent the dimensions from the activity to the service, the operation would had worked
 
Upvote 0

Shivito1

Active Member
Licensed User
That is correct. I changed it to a dip value and it worked. Thanks!!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…