Android Question [Solved] Strange error....

Discussion in 'Android Questions' started by Johan Schoeman, Mar 6, 2016.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed 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....

    Code:
    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:
    Code:
    ** 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:
     
  2. Cableguy

    Cableguy Expert Licensed 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
     
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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...
     
  4. JordiCP

    JordiCP Well-Known Member Licensed User

    Are you using raiseeventfromdifferentThread?
     
    Johan Schoeman likes this.
  5. Johan Schoeman

    Johan Schoeman Expert Licensed User

    No Jordi, using:
    Code:
    if (ba.subExists(eventName + "_brightness_changed")) {
                    ba.raiseEvent2(ba, 
    false, eventName + "_brightness_changed"true, new Object[]{cv.getBrightness()});
                  }
     
  6. JordiCP

    JordiCP Well-Known Member Licensed 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
    Code:
    if (ba.subExists(eventName + "_brightness_changed")) {
     ba.raiseEventFromDifferentThread(this, 
    null0, eventName + "_brightness_changed"true, new Object[]{cv.getBrightness()});
     }
     
    somed3v3loper and Johan Schoeman like this.
  7. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I am still confused about what happened but this solved it:
    Code:
    ba.raiseEventFromDifferentThread(getObject(), null0, eventName + "_brightness_changed"true, new Object[]{cv.getBrightness()});
    Thanks Jordi
     
    Last edited: Mar 6, 2016
    JordiCP likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice