lundi 30 novembre 2015

Android Cursor Adapter for Custom ListView Layout - Null Pointer Exception Error

First post on this site, so forgive me my sins. I am attempting to create a custom ListView layout that is populated by fields from my SQLite Database Adapter class. Below is the method I tried to use for this, and it is called in the onCreate method of its class, as well as the onClick method of a button to save to the Database:

//Method to re-populate custom list view songlist_layout when a new entry is added
    private void populateSongList() {

        //Cursor to navigate through records of the database
        Cursor cursor = myDb.getAllRows();

        //Need two arrays to work with the Cursor. First is from field names
        String [] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, 
                                                 DBAdapter.KEY_SONGTITLE, 
                                                 DBAdapter.KEY_SONGDURATION};

        //Second is int array
        int [] toViewIDs = new int [] {R.id.textViewSongNumber, R.id.textViewSongName, 
                                       R.id.textViewSongDuration};

        //Cursor Adapter Object
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), 
                      R.layout.songlist_layout, cursor, fromFieldNames, toViewIDs,0);

        //Need to grab ListView from activity_add_song to set the adapter to it
        ListView songList = (ListView)findViewById(R.id.songsListView);
        songList.setAdapter(myCursorAdapter);
    }

 //Method to handle the click event of the Save button, adding the data into the database
    public void onClickSaveSong (View v) {

        //Song Title and duration are essential fields, so we want to check if they 
        //     have text before saving to the database
        if(!TextUtils.isEmpty(etSongTitle.getText().toString()) && 
           !TextUtils.isEmpty(etSongDuration.getText().toString())) {

            myDb.insertRow(etSongTitle.getText().toString(), 
                           etSongKey.getText().toString(), 
                           etSongTuning.getText().toString(),
                           etSongDuration.getText().toString());

            //Pop-up to inform user the Data has been saved
            Toast.makeText(getBaseContext(), "Song Added!", Toast.LENGTH_LONG).show();
        }//if

        //Otherwise a pop-up to tell the user to enter the essential info
        else {Toast.makeText(getBaseContext(), "Enter Title and Duration", 
              Toast.LENGTH_LONG).show();}

        //Call to repopulate songs ListView
        populateSongList();

    }//onClickSaveSong()

The custom XML Layout for the ListView contains three TextViews to hold the songNumber, the songName and the songDuration:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textViewSongNumber"
        android:paddingRight="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textViewSongName"
        android:paddingRight="60dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textViewSongDuration"
        android:paddingRight="70dp"/>

I have read elsewhere that the problem could be because the ListView is grabbing from the wrong ListView ID ListView songList = (ListView)findViewById(R.id.songsListView); However comparing it to the XML Layout for the ListView, I don't see how this would be the case:

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/songsListView"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/addSongButton" />

Finally, the logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blob.gigstrofinal/com.blob.gigstrofinal.AddSong}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.blob.gigstrofinal.AddSong.populateSongList(AddSong.java:121)
            at com.blob.gigstrofinal.AddSong.onCreate(AddSong.java:45)

I cannot figure this out. I haven't been using Android for long, and a lot of this is over my head, so I would appreciate any help at all on this matter. The App is for a University Project, and it is due next week!

Aucun commentaire:

Enregistrer un commentaire