2
respostas

Erro na criação do método GetConnection para Android

using System;
using System.IO;
using Nutricao.Droid;
using SQLite;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_android))]
namespace Nutricao.Droid
{
    public class SQLite_android : ISqlite
    {
        public SQLite_android()
        {
        }

        public SQLiteConnection GetConnection()
        {
            var fileName = "Refeicoes.db3";
            string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var path = Path.Combine(folder, fileName);
            var connection = new SQLiteConnection(path);
            return connection;
        }
    }
}

Ao compilar para Android quando o sistema tenta fazer o deploy para o dispositivo aparece o seguinte erro:

System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception. ---> System.Exception: This is the 'bait'.  You probably need to add one of the SQLitePCLRaw.bundle_* nuget packages to your app project.
  at SQLitePCL.Batteries_V2.Init () [0x00008] in <b84e6442e7664fbaaf25b0f6f5c49250>:0
  at SQLite.SQLiteConnection..cctor () [0x00000] in <e6cd1d4f760f479ab41fbabd65b03a8d>:0
  --- End of inner exception stack trace ---
  at at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_generic_class_init (intptr)
  at Nutricao.Droid.SQLite_android.GetConnection () [0x00018] in /Users/Bruno/Documents/projetos/Alura/Alura.Xamarin/Nutricao/Droid/SQLite_android.cs:21
  at Nutricao.HomeTabbedPage..ctor () [0x0000d] in /Users/Bruno/Documents/projetos/Alura/Alura.Xamarin/Nutricao/Nutricao/HomeTabbedPage.cs:12
  at Nutricao.App..ctor () [0x00008] in /Users/Bruno/Documents/projetos/Alura/Alura.Xamarin/Nutricao/Nutricao/Nutricao.cs:10
  at Nutricao.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00024] in /Users/Bruno/Documents/projetos/Alura/Alura.Xamarin/Nutricao/Droid/MainActivity.cs:25
  at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <27c17fe440cf491ba8255bcefade6e02>:0
  at at (wrapper dynamic-method) System.Object:9da978d2-60d7-494d-8e41-e745ce27da9b (intptr,intptr,intptr)
2 respostas

Oi Bruno,

Provavelmente faltou instalar esta package no NuGet: https://www.nuget.org/packages/SQLitePCLRaw.core/1.1.1

Tenta instalar e roda novamente.

Abraço!

Já fiz isso e não funcionou continuo como o mesmo erro.