Data from a table through Android sqlite buttons

1

I'm creating a song library connected to a database The data is stored in a form (title, letter, urldelvideo) and added to a listview that only shows the title and id.

When selecting the listview item (in the listview it only shows the title) it sends me to an activity with two buttons to select to see the lyrics or to see the url of the video.

How do I pass the stored information to the views of each button? I read that with cursors but it does not work for me.

in DBHelper I have this code to get the lyrics of the song:

 public Cursor getLetra(){
    SQLiteDatabase db = this.getWritableDatabase();

    String sql = "select Letra from Canciones";
    Cursor c = db.rawQuery(sql, null);


    return c;
}

and in the activity where the lyrics will be shown:

letra_tv = (TextView) findViewById(R.id.textView4);

    Cursor res = myDb.getLetra();
    String str = "";
    if (res!=null){
        if(res.moveToFirst()){
            str= res.getString(0);
        }
    }
    letra_tv.setText(str);

The error I get is:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.inspiron.evidencia2, PID: 18385
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.inspiron.evidencia2/com.example.inspiron.evidencia2.Letra}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.inspiron.evidencia2.DBHelper.getLetra()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7406)
                  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.Cursor com.example.inspiron.evidencia2.DBHelper.getLetra()' on a null object reference
                  at com.example.inspiron.evidencia2.Letra.onCreate(Letra.java:28)
                  at android.app.Activity.performCreate(Activity.java:6904)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7406) 
                  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) 
    
asked by Ximena 03.05.2018 в 22:14
source

1 answer

-1

You are calling the getLetra() method but the value of myDb is null:

  Cursor res = myDb.getLetra();

According to your code it seems to me that the getLetra () method actually gets the value of SQLiteDatabase, so it simply calls the method to get a cursor defined by your Query:

  Cursor res = getLetra();
    
answered by 03.05.2018 / 23:21
source