SQLite Tabelle in anderer Activity auslesen

  • Antworten:20
basti38
  • Forum-Beiträge: 45

20.04.2017, 14:17:31 via Website

Hi,

ich habe mir eine SQLite Datenbank nach dem Schema des Tutorials http://www.programmierenlernenhq.de/daten-in-sqlite-datenbank-schreiben-und-lesen-in-android/#comment-17756 aufgebaut. Ich möchte aber meine Daten in einer ersten Activity eingeben und in einer zweiten ausgeben. Ich habe das bereits probiert, indem ich die dataSource der Einlese-Activity public definiert habe um sie dann in der Ausgabe-Activity zu benutzen.

Leider stürzt an dieser Stelle meine App ab.

Hat jemand eine Idee? Ich bin für jede Antwort dankbar.

Viele Grüße Basti

Antworten
swa00
  • Forum-Beiträge: 3.704

20.04.2017, 14:23:27 via Website

Hallo Basti

ohne entsprechende Detail-Angaben ( resp. Code, XML, ErrorLog ) können wir nicht helfen .

Ich gehe aber mal ganz stark davon aus, dass Du keine neue Instanz auf die DB in Activity 2 erstellst.

P.S der ErrorLog in AS sagt dir im übrigen , warum deine APp abstützt

— geändert am 20.04.2017, 14:24:31

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 14:40:19 via Website

Oh tschuldigung..

Eingabe-Acitvity (Signal-Activity):

public class SignalActivity extends AppCompatActivity implements View.OnClickListener {

    public static MeasurementMemoDataSource dataSource;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signal);
}

//Button-Definition......onclick
//.........
                    dataSource = new MeasurementMemoDataSource(this);
                    Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
                    dataSource.open();


                   MeasurementMemo shoppingMemo = 
                   dataSource.createMeasurementMemo(spinnerAuswahl, zeitpunkt);


                   Log.d(LOG_TAG, "Es wurde der folgende Eintrag in die Datenbank 
                   geschrieben:");
                   Log.d(LOG_TAG, "ID: " + measurement.getId() + ", Inhalt: " + 
                   measurementMemo.toString());


                    Log.d(LOG_TAG, "Die Datenquelle wird geschlossen.");
                    dataSource.close();

Ausgabe-Activity (DatenbankActivity):

public class DatenbankActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_datenbank);

SignalActivity.dataSource.open();

    Log.d(LOG_TAG, "Folgende Einträge sind in der Datenbank vorhanden:");
    showAllListEntries();

    SignalActivity.dataSource.close();
}

private void showAllListEntries () {
            List<MeasurementMemo> measurementMemoList =
            SignalActivity.dataSource.getAllMeasurementMemos();

            ArrayAdapter<MeasurementMemo> measurementMemoArrayAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_multiple_choice,
            measurementMemoList);

            ListView measurementMemosListView = (ListView) findViewById(R.id.list_item);
            measurementMemosListView.setAdapter(measurementMemoArrayAdapter);
}

ListView-XML:

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >


<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textStyle="bold" />

<TextView
    android:id="@+id/typ"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/zeitstempel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textStyle="bold"/>

Error-Log beim Öffnen der Datenbank-Activity:

04-20 14:39:03.913 15290-15290/com.example.standard.measuringapp V/ActivityThread: updateVisibility : ActivityRecord{a53b2c5 token=android.os.BinderProxy@c9f61f8 {com.example.standard.measuringapp/com.example.standard.measuringapp.MainActivity}} show : false

04-20 14:39:08.063 16342-16342/com.example.standard.measuringapp W/ResourcesManager: getTopLevelResources: /data/app/com.example.standard.measuringapp-1/base.apk / 1.0 running in com.example.standard.measuringapp rsrc of package null
04-20 14:39:08.113 16342-16342/com.example.standard.measuringapp W/System: ClassLoader referenced unknown path: /data/app/com.example.standard.measuringapp-1/lib/arm
04-20 14:39:08.483 16342-16342/com.example.standard.measuringapp W/System: ClassLoader referenced unknown path: /data/app/com.example.standard.measuringapp-1/lib/arm
04-20 14:39:08.753 16342-16342/com.example.standard.measuringapp W/ResourcesManager: getTopLevelResources: /data/app/com.example.standard.measuringapp-1/base.apk / 1.0 running in com.example.standard.measuringapp rsrc of package null
04-20 14:39:08.873 16342-16342/com.example.standard.measuringapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-20 14:39:09.323 16342-16342/com.example.standard.measuringapp D/SecWifiDisplayUtil: Metadata value : none
04-20 14:39:09.363 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{d8676d1 I.E...... R.....ID 0,0-0,0}
04-20 14:39:09.363 16342-16428/com.example.standard.measuringapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-20 14:39:09.433 16342-16428/com.example.standard.measuringapp I/Adreno-EGL: : EGL 1.4 QUALCOMM build: (Ia10634f51b)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 01/28/16 Thu
Local Branch: ss
Remote Branch:
Local Patches:
Reconstruct Branch:
04-20 14:39:09.433 16342-16428/com.example.standard.measuringapp D/libEGL: eglInitialize EGLDisplay = 0x9d7327c4
04-20 14:39:09.433 16342-16428/com.example.standard.measuringapp I/OpenGLRenderer: Initialized EGL, version 1.4
04-20 14:39:09.573 16342-16342/com.example.standard.measuringapp W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
04-20 14:39:09.763 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
04-20 14:39:09.793 16342-16342/com.example.standard.measuringapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@c9f61f8 time:157726961
04-20 14:39:11.983 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0
04-20 14:39:12.023 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1
04-20 14:39:12.113 16342-16342/com.example.standard.measuringapp I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
04-20 14:39:12.163 16342-16342/com.example.standard.measuringapp W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-20 14:39:12.213 16342-16342/com.example.standard.measuringapp D/AbsListView: Get MotionRecognitionManager
04-20 14:39:12.223 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@52b3d3b
04-20 14:39:12.223 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6b33c58
04-20 14:39:12.223 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6b33c58
04-20 14:39:12.253 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{55cc696 V.E...... ......I. 0,0-0,0}
04-20 14:39:12.413 16342-16342/com.example.standard.measuringapp W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
04-20 14:39:12.443 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
04-20 14:39:13.233 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0
04-20 14:39:13.293 16342-16342/com.example.standard.measuringapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1
04-20 14:39:13.363 16342-16342/com.example.standard.measuringapp I/Timeline: Timeline: Activity_launch_request id:com.example.standard.measuringapp time:157730529
04-20 14:39:13.413 16342-16342/com.example.standard.measuringapp W/ResourcesManager: getTopLevelResources: /data/app/com.example.standard.measuringapp-1/base.apk / 1.0 running in com.example.standard.measuringapp rsrc of package null
04-20 14:39:13.433 16342-16342/com.example.standard.measuringapp D/AbsListView: Get MotionRecognitionManager
04-20 14:39:13.433 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@38a9664
04-20 14:39:13.433 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@12cb0cd
04-20 14:39:13.433 16342-16342/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@12cb0cd
04-20 14:39:13.463 16342-16342/com.example.standard.measuringapp D/AndroidRuntime: Shutting down VM
04-20 14:39:13.463 16342-16342/com.example.standard.measuringapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.standard.measuringapp, PID: 16342
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.standard.measuringapp/com.example.standard.measuringapp.DatenbankActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.standard.measuringapp.MeasurementMemoDataSource.open()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.standard.measuringapp.MeasurementMemoDataSource.open()' on a null object reference
at com.example.standard.measuringapp.DatenbankActivity.onCreate(DatenbankActivity.java:78)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.04.2017, 15:00:49 via Website

Hallo Basti,

Fehler ist der hier:
Unable to start activity ComponentInfo{com.example.standard.measuringapp/com.example.standard.measuringapp.DatenbankActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.standard.measuringapp.MeasurementMemoDataSource.open()' on a null object reference

Dein MeasurementMemoDatasource Objekt in deiner anderen Actitivy ist null..

So wie du darauf zugreifst macht man das im normalfall auch nicht.
Entweder man hat eine Single-Instanz vom Objekt, dann macht man ein Singeton draus und holt sich die Instanz z.B. per Class#getInstance(potenialInitParams) etc,,

Dann kannst du von beiden Activities ohne Probleme zugreifen.
Andere Möglichkeit, du gibst dem Intent das Objekt mit, dafür muss es Serialisierbar oder so sein. Dann kannst du das Objekt aus dem Intent wiederherstellen.

Ich würde Möglcihkeit 1 bevorzugen

LG Pascal //It's not a bug, it's a feature. :) ;)

swa00

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 15:12:28 via Website

erstmal danke für die schnelle Rückmeldung.

Also mache ich jetzt aus meiner MeasurementMemoDataSource-Klasse eine Singleton-Klasse?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.04.2017, 15:17:08 via App

Genau

LG Pascal //It's not a bug, it's a feature. :) ;)

swa00

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 15:26:39 via Website

hab nun meiner MeasurementMemoDataSource-Class folgendes hinzugefügt:

public class MeasurementMemoDataSource{
private static MeasurementMemoDataSource _instance;

private MeasurementMemoDataSource() {

}

public synchronized static MeasurementMemoDataSource getInstance() {
    if (_instance == null) {
        _instance = new MeasurementMemoDataSource();
    }
    return _instance;
}

//restlicher Code
}

ist das so richtig? Sorry ich mach das zum ersten Mal..

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.04.2017, 15:28:32 via Website

Jap.

Jetzt musst du nur schauen, wegen den Threads oder warum machst du die Methode synchronized ?
Solange du keine Init Params brauchst bleibt es so einfach

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 15:31:31 via Website

Ja gut dann lösche ich das synchronized wieder..

Und wie bekomme ich jetzt meine dataSource von der Signal-Activity in meine Datenbank-Activity?

— geändert am 20.04.2017, 16:39:16

Antworten
swa00
  • Forum-Beiträge: 3.704

20.04.2017, 15:58:10 via Website

Ich schmeiss mich mal von der Seite rein :-)

@Pascal : warum lässt du ihn Singleton machen ?
Im Prinzip benötigt er doch nur ne neue Instanz in Activity 2 - sein Helper ist ja autark

MeasurementMemoDataSource dataSource_Activity2 = new MeasurementMemoDataSource(this);
Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
dataSource_Activity2.open();

— geändert am 21.04.2017, 11:39:21

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 16:39:46 via Website

habe meinen letzten Beitrag nochmal bearbeitet.. meinte natürlich die dataSource aus der Signal-Activity.

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.04.2017, 17:31:48 via Website

@Stefan - Im Prinzip hast du recht, hab oben im Code vorher übersehen, dass da ja ein DB eintrag angelegt wird und die Daten nicht nur in der Instanz hinterlegt werden ;)
Kann man da so machen, ist aber kein muss (Solange keine Komplikationen mit Threads etc. die drauf zugreien...). Am Ende kann der @TE ja auch sein Singleton wieder nurch ein new _Class() ersetzen und es ist erledigt.

@Basti: Kommt darauf an ob mit Singleton oder ohne.
Damit ich jetzt nicht dauernd zwischen 2 Konzepten hin und her springe behalte ich jetzt die Singleton bei ;)
Im onCreate deiner Activity muss du einfach deiner Variable einen Wert zuweisen.
dataSource = MeasurementMemoDataSource .getInstance();
Danach kannst du mit der Variable dataSource weiterarbeiten.

LG Pascal //It's not a bug, it's a feature. :) ;)

swa00

Antworten
basti38
  • Forum-Beiträge: 45

20.04.2017, 18:38:49 via Website

Leider stürzt die App immer noch ab..

public class DatenbankActivity extends AppCompatActivity {

MeasurementMemoDataSource dataSource;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_datenbank);





    dataSource = MeasurementMemoDataSource.getInstance();
    Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
    dataSource.open();

    Log.d(LOG_TAG, "Folgende Einträge sind in der Datenbank vorhanden:");
    showAllListEntries();

    dataSource.close();

}

private void showAllListEntries () {
    List<MeasurementMemo> measurementMemoList = dataSource.getAllMeasurementMemos();

    ArrayAdapter<MeasurementMemo> measurementMemoArrayAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_multiple_choice,
            measurementMemoList);

    ListView measurementMemosListView = (ListView) findViewById(R.id.list_item);
    measurementMemosListView.setAdapter(measurementMemoArrayAdapter);
}

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.04.2017, 18:56:21 via Website

Diesmal muss es ein anderer Fehler sein.
Kannst du den Log (rot) nochmal posten?

Stimmt die ID der ListView so? Ich glaube nicht...

ListView measurementMemosListView = (ListView)findViewById(R.id.list_item);

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
basti38
  • Forum-Beiträge: 45

21.04.2017, 09:17:06 via Website

Die ID der ListView war schon mal falsch, da hast du recht. Leider haut irgendwas immer noch nicht hin..

Hier mein ErrorLog:

04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@38a9664
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@12cb0cd
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@12cb0cd
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp D/MainActivity: Die Datenquelle wird geöffnet.
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp D/MeasurementMemoDataSource: Eine Referenz auf die Datenbank wird jetzt angefragt.
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp D/AndroidRuntime: Shutting down VM
04-21 09:13:02.874 5901-5901/com.example.standard.measuringapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.standard.measuringapp, PID: 5901
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.standard.measuringapp/com.example.standard.measuringapp.DatenbankActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.standard.measuringapp.MeasurementMemoDbHelper.getWritableDatabase()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.standard.measuringapp.MeasurementMemoDbHelper.getWritableDatabase()' on a null object reference
at com.example.standard.measuringapp.MeasurementMemoDataSource.open(MeasurementMemoDataSource.java:51)
at com.example.standard.measuringapp.DatenbankActivity.onCreate(DatenbankActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Antworten
swa00
  • Forum-Beiträge: 3.704

21.04.2017, 09:30:08 via Website

Hallo Basti,

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.standard.measuringapp/com.example.standard.measuringapp.DatenbankActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.standard.measuringapp.MeasurementMemoDbHelper.getWritableDatabase()' on a null object reference

Du hast nachwievor deine DB in Activity2 NICHT initiiert.

Kann es sein , dass du meine Beiträge ein wenig ignorierts ? :-)
Hast du denn mal den Weg realisiert , den ich oben beschrieben habe ?

a) deine DB Klasse als OOP auslagern.
b) in Activity2 machst du schlichtweg eine neue Instanz auf die DB , so wie in Actvity1
c) Ich würde darauf achten , dass du ggf die DB in Activity 1 zu machst , bevor du den Intent für Activity2 losschickst

P.S Normalerweise macht man ein open / close der DB in den Settern und Gettern - wenn man nicht in einer
Loop ständig liest/schreibt .

— geändert am 21.04.2017, 12:21:53

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten
basti38
  • Forum-Beiträge: 45

21.04.2017, 14:20:19 via Website

nein, denk bitte nicht, dass ich deine beiträge ignorier. Ich glaube eher, dass ich das nicht ganz verstanden habe.
Ich versuche das mal und melde mich dann wieder.

Außerdem habe ich deine variante auch schon probiert gehabt, so wie dus oben beschrieben hast. das hat leider auch nicht auf anhieb geklappt deswegen bin ich wieder auf die andere möglichkeit umgesprungen.

— geändert am 21.04.2017, 14:42:14

Antworten
basti38
  • Forum-Beiträge: 45

24.04.2017, 14:44:37 via Website

Also habe es bis jetzt leider noch icht hinbekommen. Ich hab nun mal versucht meine Datenbank (also den ListView) in die gleiche Klasse/Activity einzubauen. Leider bekomme ich auch hier den gleichen Fehler ("Unable to start the activity"). Bedeutet ja im Endeffekt, dass es an was anderem liegt.. oder verstehe ich das falsch?

Antworten
swa00
  • Forum-Beiträge: 3.704

24.04.2017, 14:49:19 via Website

Basti,

gehe doch mal ein wenig logisch vor und packe nicht gleich zwei sachen in ein Problem.

Schliesse in der Activity, bei der die DB lauft, deine DB mit close.
Dann rufst du das Intent für die zweite Activity auf und dort machst du nur mal GENAU das Gleiche,
was du in Activity 1 gemacht hast.

Ich zitiere meinen obigen Beitrag

MeasurementMemoDataSource dataSource_Activity2 = new MeasurementMemoDataSource(this);
Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
dataSource_Activity2.open();

und NICHTS weiters .

Dann schaust du Dir den ErrorLog an, setzt einen BreakPoint und Debuggst das Ganze mal in Ruhe durch

Lass doch mal ListView etc weg , deine Fehlermeldung von oben sagt eindeutig , dass du einen NullPointer auf die DB hast.
.
.
P.S ich habe mir nochmal dein obiges Beispiel der DB angeschaut.
So ganz entspricht es nicht dem , was man normalerweise einsetzt, da mir auch hier das eindeutige OOP fehlt.
Dieses Beispiel kommt schon eher dem gleich, wie man es richtig benutzen sollte
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

— geändert am 24.04.2017, 15:26:23

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

24.04.2017, 15:26:11 via Website

ich vermute dass in der Klasse MeasurementMemoDataSource etwas noch nicht stimmt....

LG Pascal //It's not a bug, it's a feature. :) ;)

swa00

Antworten
swa00
  • Forum-Beiträge: 3.704

24.04.2017, 15:35:55 via Website

@Pascal

Yupp, da bin ich auch eben drauf gestossen , du kannst dir das ja mal anschauen , wenn du zeit hast .

Irgendwie ein bischen "verkorkst" das Tutorial :-)

Ich vermute , dass er das DB Geraffel nicht in einer eigenen Klasse hat

— geändert am 24.04.2017, 15:39:22

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten