Search code examples
c#androidxamarin.formsapksqlite-net

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.

I'm using Visual Studio Xamarin Forms with C#, I'm getting the data from an API and a database located on a server. I tested an apk and it worked fine.

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. 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.

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.

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 :(


Solution

  • Install ADB and the ran logcat

    Here are the logs: https://pastebin.com/304eFXMq

    Basically the key line was: "SQLite.SQLiteException: no such table: 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.