I'm creating an application to train mobile development, I'm doing for Android in Visual Studio, using Xamarin. I'm having trouble listing an SQLite result.
I created two activities, one to do the registration and already list, and the other just to list the data registered in a ListView. The problem is, in the first activity, when inserting a data, it lists the others (that already exist in the database) normally, but in the second activity, when loading the app, it throws an exception: "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object occurred." The exception is in the following code snippet:
lista.Adapter = adapter;
Follow the codes:
Function that brings results:
public List<Abastece> GetAbastecimentos()
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Abastece.db")))
{
return conexao.Table<Abastece>().ToList();
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
C # code:
namespace Control_Car
{
[Activity(Label = "ListaAbast", MainLauncher = true)]
public class ListaAbast : Activity
{
Button button;
ListView lista;
DataBase db;
protected override void OnCreate(Bundle savedInstanceState)
{
CriarBancoDeDados();
base.OnCreate(savedInstanceState);
button = (Button)FindViewById(Resource.Id.bt_registrar);
lista = (ListView)FindViewById(Resource.Id.lista);
SetContentView(Resource.Layout.ListaAbast);
var listaAbast = db.GetAbastecimentos();
ArrayAdapter adapter = new ArrayAdapter<Abastece>(this,
Android.Resource.Layout.SimpleListItemMultipleChoice, listaAbast);
lista.Adapter = adapter;
lista.Activated = true;
}
private void CriarBancoDeDados()
{
db = new DataBase();
db.CriarBanco();
}
}
}
Just reinforcing, this code in C # is in the two activity, however if I try to call the function of populating the ListView in OnCreate it throws the exception. For example I will put the code of the other activity, in which I want to only list the data when inserting some other:
namespace Control_Car
{
[Activity(Label = "Abastecer", ScreenOrientation =
ScreenOrientation.Portrait)]
public class Abastecer : Activity
{
Button btn;
EditText valorKM, preco, litros, nomePosto;
ListView ltAbastecimento;
DataBase db;
string[] items = { };
List<Abastece> listaAbast = new List<Abastece>();
protected override void OnCreate(Bundle savedInstanceState)
{
CriarBancoDeDados();
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Abastecer);
btn = (Button)FindViewById(Resource.Id.bt_registrar);
valorKM = (EditText)FindViewById(Resource.Id.valor_km);
preco = (EditText)FindViewById(Resource.Id.valor_preco);
litros = (EditText)FindViewById(Resource.Id.valor_litros);
nomePosto = (EditText)FindViewById(Resource.Id.nome_posto);
ltAbastecimento = (ListView)FindViewById(Resource.Id.lista);
CarregaDados();
btn.Click += delegate
{
Abastece abastece = new Abastece()
{
ValorKM = Double.Parse(valorKM.Text),
Preco = Double.Parse(preco.Text),
Litros_qnt = Double.Parse(litros.Text),
PostoUt = nomePosto.Text
};
db.InserirAbastecimento(abastece);
CarregaDados();
};
}
private void CarregaDados()
{
listaAbast = db.GetAbastecimentos();
ArrayAdapter<Abastece> adapter = new ArrayAdapter<Abastece>(this,
Android.Resource.Layout.SimpleListItem1, listaAbast);
ltAbastecimento.Adapter = adapter;
}
private void CriarBancoDeDados()
{
db = new DataBase();
db.CriarBanco();
}
}
}