android - Xamarin Zxing Fragment OnPause Only the original thread that created a view hierarchy can touch its views -


i'm developing codebar app android xamarin , zxing library. objective have in same view half of screen codebar view, , other half buttons add or delete scanned object list.

in mainactivity oncreate function have:

        scanfragment = new zxingscannerfragment();         fragmenttransaction fragmenttx = this.fragmentmanager.begintransaction();         fragmenttx.replace(resource.id.fragment, scanfragment);         fragmenttx.settransition(fragmenttransit.fragmentfade);         fragmenttx.commit(); 

in zxingscannerfragment oncreate have

        frame = (framelayout)layoutinflater.inflate(resource.layout.zxingscannerfragmentlayout, viewgroup, false);         return frame; 

what want that, when user scans view of camera shut off, , when user has decided if wants keep or discard scanned object camera shows again.

so have scann function called when code detected on mainactivity, calls onpause method in zxing fragment , enables buttons code:

            var opts = new mobilebarcodescanningoptions {                 possibleformats = new list<zxing.barcodeformat> {                 zxing.barcodeformat.all_1d,             }             };             scanfragment.startscanning(result => {                  if (result == null || string.isnullorempty(result.text)) {                     toast.maketext(this, "scanning cancelled", toastlength.long).show();                     return;                 }                 else                 {                     _player.start();                     runonuithread(() => codbox.text = result.text);                     runonuithread(() => addbut.enabled = true);                     runonuithread(() => delbut.enabled = true);                     runonuithread(() => masbut.enabled = true);                     runonuithread(() => menbut.enabled = true);                     runonuithread(() => buttondate.enabled = true);                     runonuithread(() => finishbut.enabled = false);                     scanfragment.onpause();                 }             }, opts); 

and have function calls onresume zxing fragment.

the onpause function looks like:

      base.onpause ();       if (scanner != null)       {            frame.removeview(scanner);            if (!usecustomoverlayview)                 frame.removeview(zxingoverlay);            else if (customoverlayview != null)                 frame.removeview(customoverlayview);            scanner.shutdowncamera();            scanner = null;         } 

the problem is: code onpause function gives "only original thread created view hierarchy can touch views" exception, if ignore works random time. can take code, camera vanishes, add or remove object, , call camera again , works fine 5... 10... 15 times in row until "unhandled exception: java.lang.nullpointerexception" no idea being fired , no more info.

if prevent hierarchy exception like:

            if (scanner != null)             {                 var myactivity = (mainactivity)this.activity;                 myactivity.runonuithread(() =>                 {                     frame.removeview(scanner);                     if (!usecustomoverlayview)                         frame.removeview(zxingoverlay);                     else if (customoverlayview != null)                         frame.removeview(customoverlayview);                 });                 scanner.shutdowncamera();                 scanner = null;             } 

the camera vanishes, no exception throwed when call onresume static image of last code detected.

at end after many attempts realized using unmanaged crash correct behaviour of app.

what instead of calling onresume , onpause functions (who have objectives) of zxingfragment call directly scan function , stopscanning function.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -