Xamarin Forms - Random error - android.runtime.JavaProxyThrowable: System.ArgumentOutOfRangeExceptio

PetridisPetridis BRMember

in the last month we are facing a problem in an android application developed using Xamarin Forms. The application usually works however on devices with Android version 4.4 or lower after heavy usage it starts to display the following error

    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
    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:994)
    ... 2 more
Caused by: android.runtime.JavaProxyThrowable: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00011] in <af6732aa09ee4b909854dccb0d522f24>:0 
  at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <af6732aa09ee4b909854dccb0d522f24>:0 
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x0000c] in <af6732aa09ee4b909854dccb0d522f24>:0 
  at Xamarin.Forms.NavigationProxy.PopModal () [0x00012] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.NavigationProxy.OnPopModal (System.Boolean animated) [0x00012] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.NavigationProxy.PopModalAsync () [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at AppTailler.SelecionarMotivos+<SalvarClicked>d__13.MoveNext () [0x0036b] in <014096e14bed4164874e3d1600ae8a7c>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <af6732aa09ee4b909854dccb0d522f24>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in <af6732aa09ee4b909854dccb0d522f24>:0 
  at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in <a5074c74656e4a848b5b501002439cbc>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <a5074c74656e4a848b5b501002439cbc>:0 
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <a5074c74656e4a848b5b501002439cbc>:0 
  at (wrapper dynamic-method) System.Object:5567cc09-fb12-48c3-9293-77a23494730a (intptr,intptr)
    at mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
    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:4895)
    ... 5 more

When reinstalling the application in these versions it returns to normal operation, and after a time it again causes the same error.

Here is the line of code where the error happens.

public async void SalvarClicked(object sender, EventArgs e)
    {
        if (motivoSelecionado == null
            && String.IsNullOrEmpty(escreverMotivo.Text)
            && (motivosSelecionados.Any() || motivosSelecionados == null || motivosSelecionados.Count == 0))
        {
            await DisplayAlert("Alerta", "Você deve inserir pelo menos um motivo.", "Sim");
            return;
        }
        else
        {
            using (var dados = new DataAccess())
            {
                if (!String.IsNullOrEmpty(escreverMotivo.Text))
                {
                    var motivoNovo = await DisplayAlert("Alerta", "Você está inserindo um motivo novo deseja continuar?", "Sim", "Não");
                    if (!motivoNovo)
                    {
                        return;
                    }
                    else
                    {
                        if (motivoSelecionado == null)
                        {
                            motivos mot = new motivos()
                            {
                                IdTextoVistoria = 0,
                                idSubItem = 0,
                                txtNumTexto = 0,
                                txtTexto = escreverMotivo.Text
                            };
                            motivoSelecionado = mot;
                        }
                        motivoSelecionado.IdTextoVistoria = 0;
                        motivoSelecionado.txtTexto = escreverMotivo.Text;
                    }
                }
                auditoria auditoria = dados.GetAuditoria(idAudit);
                auditoria.IdAuditoria = auditoria.IdAuditoria;
                if (motivoSelecionado != null)
                {
                    auditoria.SubDescId = motivoSelecionado.IdTextoVistoria;
                    auditoria.SubDescTexto = motivoSelecionado.txtTexto;
                }
                auditoria.SubPhotoUrl = auditoria.SubPhotoUrl;
                auditoria.SubMotivoBytes = auditoria.SubMotivoBytes;
                if (_mediaFile != null)
                {
                    var stream = _mediaFile.GetStream();
                    targetImageByte = ReadFully(stream);
                    auditoria.SubMotivoBytes = targetImageByte;
                }
                dados.AtualizarAuditoria(auditoria);
                await DisplayAlert("Sucesso", "Salvo com sucesso!", "Ok");
            }
        }
        await App.NP.Navigation.PopModalAsync();
    }

I do not know if it is a problem in the code, or if the version of android is influencing it.

Anyway any help is welcome

Sign In or Register to comment.