Android Question java.lang.reflect.InvocationTargetException

zekigultekin

Member
Licensed User
Longtime User
how can solve "java.lang.reflect.InvocationTargetException" problem

my code like this

B4X:
#Region  Project Attributes
    #ApplicationLabel: testprint
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#AdditionalJar: customandroidapi
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim serial1 As Serial
    Dim r As Reflector
    Dim prnDevice As JavaObject
    'Dim adapter As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    serial1.Initialize("Serial1")
   
    'Serial1 is an initialized Serial object
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim image As Bitmap
    image.Initialize(File.DirAssets,"test.png")
    r.Target = serial1
    Dim adapter As JavaObject = r.GetField("blueAdapter")
    adapter = r.GetField("blueAdapter")
    'Dim BluetoothDevice As JavaObject = adapter.RunMethodJO("getRemoteDevice", Array("50:E8:00:F5:89:5C"))
    Dim BluetoothDevice As JavaObject = adapter.RunMethod("getRemoteDevice", Array("10:00:E8:6C:8D:45"))
   
    'Dim prnDevice As JavaObject
    prnDevice = prnDevice.InitializeNewInstance("it.custom.printer.api.android.CustomAndroidAPI", Null).RunMethod( _
    "getPrinterDriverBT", Array(BluetoothDevice))

   
'    Dim CustomPrinter As JavaObject
'    CustomPrinter.InitializeStatic("it.custom.printer.api.android.CustomPrinter")
'    prnDevice.RunMethod("printImage", Array(image, CustomPrinter.GetField("IMAGE_SCALE_TO_WIDTH"),CustomPrinter.GetField("CustomPrinter.IMAGE_SCALE_TO_FIT"),0))
End Sub
Sub Button2_Click
    Dim image As Bitmap
    image.Initialize(File.DirAssets,"test.png")
    Dim CustomPrinter As JavaObject
    CustomPrinter.InitializeStatic("it.custom.printer.api.android.CustomPrinter")
    prnDevice.RunMethod("printImage", Array(image, CustomPrinter.GetField("IMAGE_SCALE_TO_WIDTH"),CustomPrinter.GetField( _
    "CustomPrinter.IMAGE_SCALE_TO_FIT"),0))
End Sub
 

Shadow&Max

Active Member
Licensed User
Longtime User
Hi Erel...

Having a small issue with this too... It works almost always, but a tester of mine has a picture that almost always causes the error. It's definitely a memory error, and there's not much I can do about it as it's only happening in certain circumstances on certain devices (mostly tablets), but what I'd like to do is trap the error cause it's causing other things to blow up unnecessarily... Here's the code:

B4X:
Sub RotateImage(original As Bitmap, degree As Float) As Bitmap
   Dim matrix As JavaObject
   matrix.InitializeNewInstance("android.graphics.Matrix", Null)
   matrix.RunMethod("postRotate", Array(degree))
   hw = original.Width
   hh = original.Height
   Dim bmp As JavaObject
   bmp.InitializeStatic("android.graphics.Bitmap")
   Dim NewImage As Bitmap
   'The Error Is Here!

   NewImage = bmp.RunMethod("createBitmap", Array(original, 0, 0, original.Width, original.Height, matrix, True))


   Return NewImage
End Sub

Of course, the error happens on that "NewImage =" line

Here's the log info:

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:109)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
... 25 more
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:109)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
... 25 more

I can't attach the photo cause its 800+K, but it stays the same size, almost when zipped... Should I email the photo to you?

It's a strange one. He took it on a digital camera and transferred it over to his device. When I downloaded it, it came rotated counter-clockwise. When I choose it from the gallery, it's shown as portrait... when it gets inserted into my ImageView, it's rotated counter-clockwise again. Then, I try to rotate it and get the error... Awfully bizarre.

What's the best way to trap that error??? This is an odd case, but I want to make sure that if someone else does it, it won't kill everything else they're doing at the time.Thanks in advance...
 
Last edited:
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Erel... I'll use the Try/Catch block... I have no issues with pictures much bigger than this, and I have determined that this SPECIFIC picture has some bizarre things going on, and it is not by any means the norm... As long as I catch the error, and redirect the issue, I'll be fine.

Just to be sure... Does Try/Catch trap ANY error that Android can spit up?
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Well, I tried it but now am getting another message I don't understand... Here's the code:

B4X:
Try
     NewImage = bmp.RunMethod("createBitmap", Array(original, 0, 0, original.Width, original.Height, matrix, True))
        Return NewImage
Catch
    Msgbox2("There's not enough internal memory on your device to perform this operation or there's a specific problem with this image." & CRLF & CRLF & "Your existing image will be restored.","Device Memory Error!","","Ok","",LoadBitmap(File.DirAssets,"ic_stop.png"))
    Return original
End Try

The error message that's now displayed is:

An error has occurred in sub:
java.lang.ClassCastException: java.lang.
OutOfMemoryError cannot be cast to java.lang.Exception
Continue
Yes No

Don't have a clue what this means...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I think you cannot catch an OOM with try except.
The OOM cannot be casted to an Exception
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Manfred, but if that's the case, is there any way to trap that error?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thanks Manfred, but if that's the case, is there any way to trap that error?

Don´t use so much memory :D (sorry, couldn´t resist ;-) )
I fact; no i cant help... working with images causes a lot of memory need. In b4a i would say search the forum for bitmapsampled (or so) but i´m really not familar with reflection and/or javaobject and in detail even with JAVA to give you an advice here. Sorry
 
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
You're a funny guy Manfred lol ;)!

The reality is that this error has never popped up except with ONE picture... and it's a sweet picture, but he's manipulated it, and everything about it is weird... He took it with a tablet. Of course, even though he had rotated the tablet when he took the picture but the tablet saved it as rotated, landscape sideways! He edited the pic, and made it portrait. Obviously didn't take because when I downloaded it from him, it came to my downloads folder as landscape, rotated counter-clockwise. it's 800K. When I tried to zip it, the zip came out as 800K! I tried to rotate it and it shot the error. Then I tried another that he sent, and it worked fine.

I guess I won't worry about this then... If someone else complains, then I'll tackle it again.

Thanks bud... Cheers Manfred :)

Enis
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try / Catch doesn't catch errors only exceptions. Errors (such as memory errors) are not meant to be caught. I though that it will catch this one based on the stack trace however I was wrong.

Technically it is simple to catch memory errors with a small library. LoadBitmap itself catches such errors and then downsamples the bitmap automatically.
 
Upvote 0
Top