Android Question [Solved] Strange error....

Johan Schoeman

Expert
Licensed User
Longtime User
I have an activity with a customview, 3 buttons, and a label. I have only just added the label via designer. In the library I raise an event....

B4X:
Sub zx1_brightness_changed (bright As Double)                 'ADDED 6 MARCH 2016
    Label1.Text = "" & bright
    If bright < 0.30 Then zx1.brightnessTurnFlashOn
    If bright > 0.35 Then zx1.brightnessTurnFlashOff
   
End Sub
....but when the Event is raised I get this error:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_zx1_brightness_changed (java line: 432)
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6984)
    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1104)
    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4542)
    at android.view.View.invalidate(View.java:11651)
    at android.view.View.invalidate(View.java:11605)
    at android.widget.TextView.checkForRelayout(TextView.java:8054)
    at android.widget.TextView.setText(TextView.java:4876)
    at android.widget.TextView.setText(TextView.java:4729)
    at android.widget.TextView.setText(TextView.java:4704)
    at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:43)
    at JHS.ZxingBarcodeScanner.main._zx1_brightness_changed(main.java:432)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at zxingbarcodescannerwrapper.zxingBarcodeScannerWrapper$2.brightness_changed(zxingBarcodeScannerWrapper.java:188)
    at main.java.me.dm7.barcodescanner.zxing.ZXingScannerView.invokeBrightnessChanged(ZXingScannerView.java:241)
    at main.java.me.dm7.barcodescanner.zxing.ZXingScannerView.buildLuminanceSource(ZXingScannerView.java:186)
    at main.java.me.dm7.barcodescanner.zxing.ZXingScannerView.onPreviewFrame(ZXingScannerView.java:129)
    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1005)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.os.HandlerThread.run(HandlerThread.java:61)
** Activity (main) Pause, UserClosed = true **

If I remove Label1.Text = "" & bright from the code then all is working...What am I missing here?:confused:
 

Cableguy

Expert
Licensed User
Longtime User
Are you sure the event on the custom view is not named the same as your calling event? From the logs I would say you have the same names for both the custom view managed event and for the "owner" module called event
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Are you sure the event on the custom view is not named the same as your calling event? From the logs I would say you have the same names for both the custom view managed event and for the "owner" module called event
The event name is set in the customview as zx1. All events are raised without a problem from within the library. All that I am trying to do is to display a returned value in the label by setting the text of the label. This does not make sense to me whatsoever...
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Are you using raiseeventfromdifferentThread?
No Jordi, using:
B4X:
              if (ba.subExists(eventName + "_brightness_changed")) {
                ba.raiseEvent2(ba, false, eventName + "_brightness_changed", true, new Object[]{cv.getBrightness()});
              }
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Not totally sure since I haven't tried it, but raiseEvent/2 will fail if called from a different thread, or if your main Activity is paused
B4X:
if (ba.subExists(eventName + "_brightness_changed")) {
 ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_brightness_changed", true, new Object[]{cv.getBrightness()});
 }
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Are you using raiseeventfromdifferentThread?
I am still confused about what happened but this solved it:
B4X:
ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_brightness_changed", true, new Object[]{cv.getBrightness()});

Thanks Jordi
 
Last edited:
Upvote 0
Top