在上个月我们在使用Xamarin Forms开发的Android应用程序中遇到问题。该应用程序通常适用于Android 4.4或更低版本的设备,在大量使用后它开始显示以下错误
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
在这些版本中重新安装应用程序时,它会恢复正常操作,一段时间后再次导致同样的错误。
以下是发生错误的代码行。
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();
}
我不知道代码中是否存在问题,或者android版本是否影响它。
无论如何,欢迎任何帮助