Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
SQLite Datenbank in Kotlin - App stürtzt immer ab
#1
Hey,

ich bin neu in Java bzw. Kotlin und Co.
dabei bin ich bei einem Übungsprojekt auf ein Problem gestoßen was ich selbst nicht lösen kann. Ich habe "gegoogelt" wie ein irrer und finde den Fehler nicht.


Zu meiner App:
Es ist eine simple Einkaufsliste. Auf der MainActivity soll eine Liste mit den Einkäufen angezeigt werden die ein der NewActivity erzeugt werden.
Die Daten sollen einzeln und alle zusammen gelöscht werden können.

später sollen vielleicht noch einzelne Einkaufzettel erstellt werden können also Kategorien oder so.



Das Problem:
Aus irgend einem Problem, Stürzt die App schon beim Start ab, ohne eine Fehlermeldung aus zu geben.
Der Fehler ist irgendwo im SQLite-Code aber ich hab keine Ahnung was ich falsch mache.
ich hoffe ihr könnt mir weiterhelfen.

Die Projekt Dateien: http://uploaded.net/file/y4r4gdgm

MainActivity:


Code:
package com.example.einkaufsliste

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {


   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       // Ausgabe der Tabelle
       TableHelper(this).alleEintraege()


       // Button Neu (btn_New)
       btn_new.setOnClickListener {
           val btnNew = Intent(this, NewActivity::class.java)
           startActivity(btnNew)
       }

   }

   override fun onCreateOptionsMenu(menu: Menu): Boolean {
       val inflater = menuInflater
       inflater.inflate(R.menu.main_menu, menu)
       return true
   }

   //Button(Menu) Zurück zur MainActivity
   override fun onOptionsItemSelected(item: MenuItem): Boolean {
       when (item.itemId) {
           R.id.btn_info -> {
               info("")
               startActivity(intent)
               return super.onOptionsItemSelected(item)
           }
           else -> return super.onOptionsItemSelected(item)
       }
   }
   private fun info(msg: String) {
       Toast.makeText(this, msg, Toast.LENGTH_LONG)
       intent = Intent(this, InfoActivity::class.java)
   }


}


NewActivity:

Code:
package com.example.einkaufsliste

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_new.*
import java.util.jar.Attributes


class NewActivity : AppCompatActivity() {



   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_new)

       btn_aktion.setOnClickListener {
           val dbHandler = MindOrksDBOpenHelper(this, null)
           val user = Attributes.Name(text_einkauf_eingabe.text.toString())
           dbHandler.addEinkauf(user)
           Toast.makeText(this, text_einkauf_eingabe.text.toString() + " auf die Liste", Toast.LENGTH_LONG).show()
       }

   }


       override fun onCreateOptionsMenu(menu: Menu): Boolean {
           val inflater = menuInflater
           inflater.inflate(R.menu.menu_back, menu)
           return true
       }

       //Button(Menu) Zurück zur MainActivity
       override fun onOptionsItemSelected(item: MenuItem): Boolean {
           when (item.itemId) {
               R.id.btn_back -> {
                   back("")
                   startActivity(intent)
                   return super.onOptionsItemSelected(item)
               }
               else -> return super.onOptionsItemSelected(item)
           }
       }
       private fun back(msg: String) {
           Toast.makeText(this, msg, Toast.LENGTH_LONG)
           intent = Intent(this, MainActivity::class.java)
       }
}

[b]DatenbankKlasse:[/b]

Code:
package com.example.einkaufsliste

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper


class DatenbankKlasse(context: Context):
   SQLiteOpenHelper(context, "Einkaufsliste.db", null, 1) {

   override fun onCreate(db: SQLiteDatabase) {
       db.execSQL("CREATE TABLE [einkaufsliste]" +
               " ([id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
               "[einkauf] TEXT NOT NULL, [wert] INTEGER NOT NULL)")
   }

   override fun onUpgrade(db: SQLiteDatabase,
                          versionAlt: Int, versionNeu: Int) {
   }
}


[b][b]Einkauf:[/b][/b]

Code:
package com.example.einkaufsliste


class Einkauf  {
   var id: Int = 0
   var meinEinkauf: String? = null
   constructor(id: Int, meinEinkauf: String) {
       this.id = id
       this.meinEinkauf = meinEinkauf
   }
   constructor(meinEinkauf: String) {
       this.meinEinkauf = meinEinkauf
   }
}


[b][b][b]TableHelper:[/b][/b][/b]

Code:
package com.example.einkaufsliste

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.os.Build
import android.os.Build.ID
import android.util.Log
import com.example.einkaufsliste.MindOrksDBOpenHelper.Companion.COLUMN_EINKAUF
import com.example.einkaufsliste.MindOrksDBOpenHelper.Companion.TABLE_EINKAUF
import java.net.IDN

class TableHelper( context: Context) {

   private val helper = MindOrksDBOpenHelper(context, factory = null)

   fun speichereNeuenEintrag(daten: Einkauf): Long {
       //Öffnet eine beschreibbare Datenbank
       val db = helper.writableDatabase
       //Ein Datensatz ist eine Zeile in der SQLite Datenbank
       val datensatz = ContentValues()
       datensatz.put(COLUMN_EINKAUF, daten.meinEinkauf)

       db.beginTransaction()
       //Der Rückgabewert, also die ID des gespeicherten Objekts wird hier zurückgegeben.
       val id: Long = try {
           val id = db.insert(TABLE_EINKAUF, null, datensatz)
           db.setTransactionSuccessful()
           id
       }
       //Wenn die Operation beendet ist: empfange den Rückgabewert
       finally {
           db.endTransaction()
           db.close()
       }
       Log.i("test", "Datensatz in TABLE_Einkauf eingefügt. $id")
       return id
   }
   fun alleEintraege(): ArrayList<Einkauf>{
       val auswahl = arrayOf(TABLE_EINKAUF)
       val db = helper.writableDatabase
       val cursor = db.query(COLUMN_EINKAUF, auswahl, null, null, null, null, null)

       val ergebnisListe = ArrayList(alleEintraege())
       while (cursor.moveToNext()){
           var rueckgabe = Einkauf(0,"")
           rueckgabe.id = cursor.getInt(cursor.getColumnIndex(Build.ID))
           ergebnisListe.add(rueckgabe)
       }
       Log.i("test", "Ergebnisliste Alle Jobs enthält: $ergebnisListe")
       cursor.close()
       return ergebnisListe
   }
   fun loescheDatensatz(id :Int){
       val db = helper.writableDatabase
       db.delete(TABLE_EINKAUF,"${Build.ID}  = $id", null)
   }

}

[b][b][b][b]MindOrksDBOpenHelper:[/b][/b][/b][/b]

Code:
package com.example.einkaufsliste

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import java.util.jar.Attributes

class MindOrksDBOpenHelper(context: Context,
                          factory: SQLiteDatabase.CursorFactory?) :
   SQLiteOpenHelper(context, DATABASE_EINKAUF, null
       , DATABASE_VERSION) {
   override fun onCreate(db: SQLiteDatabase) {
       val TABLE_EINKAUF = ("CREATE TABLE " +
               TABLE_EINKAUF + "("
               + COLUMN_ID + " INTEGER PRIMARY KEY," +
               COLUMN_EINKAUF
               + " TEXT" + ")")
       db.execSQL(TABLE_EINKAUF)

   }

   override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
       db.execSQL("DROP TABLE IF EXISTS " + TABLE_EINKAUF)
       onCreate(db)

   }

   fun addEinkauf(einkauf: Attributes.Name) {
       val values = ContentValues()
       values.put(COLUMN_EINKAUF, einkauf.javaClass.toString())
       val db = this.writableDatabase
       db.insert(TABLE_EINKAUF, null, values)
       db.close()
   }
   fun getAllName(): Cursor? {
       val db = this.readableDatabase
       return db.rawQuery("SELECT * FROM $TABLE_EINKAUF", null)
   }
   companion object {
       private val DATABASE_VERSION = 1
       private val DATABASE_EINKAUF = "Einkaufsliste.db"
       val TABLE_EINKAUF = "Einkauf"
       val COLUMN_ID = "id"
       val COLUMN_EINKAUF = "meinEinkauf"
   }
}


[b][b][b][b]Ich bedanke mich im Voraus..[/b][/b][/b][/b]
Zitieren
#2
Growing fan of this forum :) Btw, if you want to know how to buy the best nasal dilators online, I highly suggest this super nasale dilatador for nostrils forum along with this top nariz dilator for nostril website and this top nasal dilatador for nostrils site and don't forget this best nasal dilatatore for schnarchen, not to mention this best nasal dilatatore for nostrils forum alongside this superiore nasal dilator for russare website and this grande nasal dilatatore for schnarchen page and don't forget this best nasali dilatatore for nostril and this grande nose dilator for snoring site along with this super nasal dilatatore for nostrils forum too! Don't forget top nasal dilatador for russare page along with this best nasal dilatador for schnarchen site and this super nostril dilatatore for nostrils site and don't forget this migliore nariz dilator for snoring as well as this great noson dilatatore nasale forum along with this great noson nariz tapada forum and this migliore noson dilatatore profile and don't forget this superiore noson nasenpflaster gegen schnarchen, not to mention this migliore noson ekoy site alongside this top noson dilatatore nasale page and this superiore noson dilatatore website and don't forget this grande noson dilatatore nasale and this migliore noson nasenpflaster gegen schnarchen forum along with this great noson cerotti nasali website too, not forgetting this grande noson dilatatore profile, as well as best noson nasenpflaster gegen schnarchen page and finally migliore noson ekoi site for good measure.

I would be remiss not to suggest this awesome nasal dilator reading along with this essential nasal dilator website and this wonderful cerotti nasali article and don't forget this wonderful cerotti nasali web site , not to mention this cheap nasal forum alongside this top nasal page and this cheap nasal url and don't forget this awesome ekoi website and this best ekoi website along with this cheap ekoi web site too! Also check out this recommended nasal dilator website along with this good patency definition web site and this essential patency definition site and don't forget this cheap patency definition site , not to mention this recommended dilatatore website along with this top rated dilatatore forum and this best dilatatore page and finally this top rated cerotti nasali reading is great too!
Zitieren
#3
Growing fan of this page :) Btw, if you want to know the best taxis to hire in the UK, I highly recommend this great taxi cab for hire forum along with this friendly cabs for hire page and this top taxi for hire website and don't forget this best taxis to hire, not to mention this friendly taxi cab to hire forum.
Zitieren
#4
Love checking out this article :) Btw, if you want to see the best info about the Mastiff breed of dog, I highly recommend this check out your url about mastiffs along with this visit on mastiffs and this this article about mastiffs and don't forget this see this website about mastiff dogs , not to mention this browse around this web-site about mastiffs alongside this read here on the mastiff breed and this sell on mastiffs and don't forget this read this article about the mastiff breed and this the advantage about the mastiff breed along with this wonderful info about mastiff dogs too! Also worth mentioning visit their website about the mastiff breed together with this why not look here about mastiffs , it's wonderful info.
Zitieren
#5
Loving the insights of this blog :) Btw, if you want to see the best info about the Mastiff type of dog, I highly suggest this check his on dogs along with this try this web-site on dog breeds and this knowing it about dog training and don't forget this why not find out more about dogs , not to mention this visit this site on dog breeds alongside this informative post on dogs and this review on dog training and don't forget this helpful hints funny dog pics and this inquiry on dog training along with this read the article on dog health too! Also worth mentioning click here now on dog breeds together with this web link on dog training , it's the best info.
Zitieren
#6
Big fan of this this page :) Btw, if you want to know the best online casino, I highly suggest this baik situs domino99 online terbaik site as well as this bagus download domino99 apk page is important too.

Love a good read of this blog :) Btw, if you want to know the best online casino, I highly recommend this teratas download domino99 apk forum as well as this terbaik main domino99 online site is important too.
Zitieren
#7
Pressfarm is really a media database that helps entrepreneurs find writers to publish about their startups. Construct a set of reporters and get shown in start-up sites in minutes. Using our media outreach platform, you can now seek out editors from various markets, guides and types to find a very good fit for the story.

Pressfarm may also help create mail pitches, press releases, material marketing and much more.

Pressfarm think that thinning down the field to journalists who're apt to be interested in your startup is much better than giving mass emails.
Zitieren
#8
Pressfarm is just a press database that helps entrepreneurs discover journalists to publish about their startups. Construct a set of reporters and get shown in startup websites in minutes. Using our media outreach program, now you can seek out writers from various niches, textbooks and categories to find the best match for the story.

Pressfarm will also support write mail pitches, press releases, material advertising and significantly more.

Pressfarm believe that narrowing down the subject to journalists who are probably be enthusiastic about your start-up is a lot better than sending bulk emails.
Zitieren
#9
Pressfarm is really a press repository that assists entrepreneurs find editors to write about their startups. Construct a listing of reporters and get stated in startup directories in minutes. Applying our media outreach system, you can now seek out journalists from numerous niches, journals and groups to find the best match for your story.

Pressfarm may also help write e-mail pitches, push releases, material marketing and significantly more.

Pressfarm believe that narrowing down the field to journalists that are probably be interested in your startup is much better than sending mass emails.
Zitieren
#10
Loving the insights of this article :) Btw, if you're interested in great Indonesian casinos, I very much recommend this baik joker388 forum along with this terbaik login joker123 profile and this baik situs joker123 page. Also, don't forget this teratas joker123, not to mention this hebat slot joker123 site alongside this teratas joker388 page. There's also this terbaik joker123 deposit pulsa profile and don't forget this hebat joker123 slot, as well as bagus joker123 deposit pulsa page and I shouldn't leave out this terbaik situs joker123 website is a good one too.
Take a look at bagus joker123 page along with this baik joker123 deposit pulsa site and this baik situs joker123 forum. Also, don't forget this baik slot joker123, not to mention this baik slot joker123 forum alongside this bagus joker388 page. There's also this baik joker123 slot website and don't forget this teratas joker123, as well as terbaik joker388 website and I shouldn't leave out this baik joker388 page is a good one too. Also check out Take a look at hebat slot joker123 website along with this terbaik joker123 slot site and this teratas joker123 slot site. Also, don't forget this terbaik joker123 slot, not to mention this teratas joker123 profile as well. Love a good read of this forum :) 48d7a37 here.
Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste