【问题标题】:System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exceptionSystem.TypeInitializationException:“SQLite.SQLiteConnection”的类型初始化程序引发异常
【发布时间】:2017-08-17 05:24:41
【问题描述】:

我是 android 和 xamarin 的新手。最近我在xamrarin 示例TaskyPortable 之后创建了一个android 应用程序。问题是每当执行以下代码时都会引发错误。

        public override void OnCreate()
        {
            base.OnCreate();
            var sqliteFilename = "ToDoItemDB.db3";
            string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(libraryPath, sqliteFilename);
            conn = new SQLiteConnection(path);

每当它使用给定路径调用新的 SQLiteConnection 时,它都会抛出

"System.TypeInitializationException:类型初始化器 'SQLite.SQLiteConnection' 抛出异常。"

我做错了什么?代码几乎类似于 TaskyPortable。 我搜索了很多,但没有找到任何东西。

请帮忙。

附: :这个问题已经被问到here 并且我已经检查了答案。我还在 droid 项目中添加了 SQLite.net 的引用。但仍然面临这个问题。

【问题讨论】:

  • 您的代码是 Xamarin Android 代码,但示例和问题是 Xamarin PCL 项目。您能确认您使用的是哪种项目吗? Xamarin PCL 还是 Xamarin Andorid?

标签: c# android sqlite xamarin xamarin.android


【解决方案1】:

此代码正在运行

  private SQLiteConnection conn;      

  protected override void OnCreate(Bundle bundle)
  {
   base.OnCreate(bundle);
   Forms.Init(this, bundle);

   var sqliteFilename = "ToDoItemDB.db3";
   string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
   var path = Path.Combine(libraryPath, sqliteFilename);
   conn = new SQLiteConnection(path);

   //create table
   conn.CreateTable<Todo>();

   LoadApplication(new App());
  }

检查这些包是否在 package.config 中。也许最好删除 Sqlite 包并只安装 sqlite-net-pcl,它正在安装其余的 sqlite 包

  <package id="sqlite-net-pcl" version="1.3.1" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.bundle_green" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.core" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.lib.e_sqlite3.android" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.provider.e_sqlite3.android" version="1.1.2" targetFramework="monoandroid60" />

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2016-11-30
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多