简体   繁体   English

在 Xamarin Forms 中从 APK 安装时应用程序崩溃

[英]App crashed when installed from APK in Xamarin Forms

App crashed when installed from APK ,but runs fine when connected via USB from Visual Studio.从 APK 安装应用程序时崩溃,但从 Visual Studio 通过 USB 连接时运行正常。

I'm using Visual Studio Xamarin Forms with C#, I'm getting the data from an API and a database located on a server.我正在使用带有 C# 的 Visual Studio Xamarin Forms,我从 API 和位于服务器上的数据库获取数据。 I tested an apk and it worked fine.我测试了一个 apk,它运行良好。

When it stopped working: Now I have made some changes, because my app has to simulate working the same when there's no internet connection, so I implemented SQLite using nuget sqlite-net-pcl.当它停止工作时:现在我做了一些更改,因为我的应用程序必须在没有互联网连接时模拟相同的工作,所以我使用 nuget sqlite-net-pcl 实现了 SQLite。 So when my app has internet connection, the app will store some values in local database, and if at any moment detects no internet will go and take the lastest info from the local database.因此,当我的应用程序有互联网连接时,该应用程序将在本地数据库中存储一些值,如果在任何时候检测到没有互联网,它将从本地数据库中获取最新信息。 I have been testing using my phone connected via USB and works great.我一直在使用通过 USB 连接的手机进行测试,效果很好。

Problem: So I created the Apk file again and after installing it shows data from the API, but when I clicked the first button it crashes or just closes without any message.问题:所以我再次创建了 Apk 文件,安装它后显示来自 API 的数据,但是当我单击第一个按钮时,它崩溃或只是关闭而没有任何消息。

My code: I don't know what can be really usefull here but basically this is my local database connection (this code line appears in every page):我的代码:我不知道什么在这里真正有用,但基本上这是我的本地数据库连接(此代码行出现在每个页面中):

private SQLiteConnection db = new SQLiteConnection(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LocalDB.db3"));

This is an example of how I insert and get data from the local database:这是我如何从本地数据库插入和获取数据的示例:

    private void InsertPisterosLocal()
    {
        //first delete table data
        //db.DeleteAll<T_Pisteros>();
        db.DropTable<T_Pisteros>();
        db.CreateTable<T_Pisteros>();

        foreach (var item in PisterosLista)
        {
            var DatosRegistro = new T_Pisteros
            {
                PisteroID = item.PisteroID,
                PisteroN = item.PisteroN
            };
            var num = db.Insert(DatosRegistro);
        }
    }

    public void GetPisterosLocal()
    {
        try
        {
            db.CreateTable<T_Pisteros>();
            List<T_Pisteros> PisterosLista = new List<T_Pisteros>();
            PisterosLista = db.Table<T_Pisteros>().ToList();
            pck_Pisteros.ItemsSource = PisterosLista;
        }
        catch (Exception)
        {
            throw;
        }
    }

And this is the manifest (again I don't know if can be of any help):这是清单(我不知道是否有任何帮助):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.gearsofsoftware.servlottery" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="Premios Servicentro" android:icon="@mipmap/icon"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

I'm really praying someone can help me here, I feel like this is a dead end for me, I have no clue what to do :(我真的在祈祷有人能在这里帮助我,我觉得这对我来说是一个死胡同,我不知道该怎么做:(

Install ADB and the ran logcat安装 ADB 并运行 logcat

Here are the logs: https://pastebin.com/304eFXMq以下是日志: https : //pastebin.com/304eFXMq

Basically the key line was: "SQLite.SQLiteException: no such table: T_Promo"基本上关键行是:“SQLite.SQLiteException:没有这样的表:T_Promo”

I thought no calls to the local database had to be done at that point, a call was being made as starting the second page (when it crashed) and this call has the following code:我认为此时不必调用本地数据库,在启动第二页时(当它崩溃时)进行调用,并且该调用具有以下代码:

db.DeleteAll<T_Promo>();
db.CreateTable<T_Promo>();

I changed it to我把它改成

db.DropTable<T_Promo>();
db.CreateTable<T_Promo>();

and now is working fine.现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM