Android 自定义 ListView 布局的光标适配器 - 空指针异常错误

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

第一次post在这个网站上,所以请原谅我的罪过。我正在尝试创建一个自定义 ListView 布局,该布局由我的 SQLite 数据库适配器 class 中的字段填充。下面是我尝试使用的方法,它在其 class 的 onCreate 方法中调用,以及保存到数据库的按钮的 onClick 方法:

//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()

ListView 的自定义 XML 布局包含三个 TextView,用于保存歌曲编号、歌曲名称和歌曲持续时间:

<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"/>

我在别处读到问题可能是因为 ListView 从错误的 ListView ID 抓取 ListView songList = (ListView)findViewById(R.id.songsListView); 但是,将它与 ListView 的 XML 布局进行比较,我不明白会是这样:

<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" />

最后,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.access0(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)

我想不通。我使用 Android 的时间不长,很多事情都在我的脑海中,所以我将不胜感激在这件事上的任何帮助。该应用程序用于大学项目,下周到期!

编辑:我忘了指定,空指针异常指向 AddSong.java 的第 121 行,即:songList.setAdapter(myCursorAdapter);

我正在从 AddSong class 中的 onCreate 方法调用 populateSongList() 方法:

public class AddSong extends Activity {

    //Declare Database Adapter
    DBAdapter myDb;

    //Declare EditText objects to be used for each field in the SQLite Database
    EditText etSongTitle;
    EditText etSongKey;
    EditText etSongTuning;
    EditText etSongDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_song);


        //Initialise EditText objects by assigning them to the corresponding ID
        etSongTitle = (EditText)findViewById(R.id.editSongTitle);
        etSongKey = (EditText)findViewById(R.id.editSongKey);
        etSongTuning = (EditText)findViewById(R.id.editSongTuning);
        etSongDuration = (EditText)findViewById(R.id.editSongDuration);

        //Call method to open Database
        openDB();
        //Call to populate songs ListView
        populateSongList();
    }

再次用于 AddSong.java 的 onClickSaveSong 方法(参见原始 post 的第二个代码片段。

EDIT2:activity_add_song.xml的全部内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.blob.gigstrofinal.AddSong"
    android:background="@color/colorPrimary"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvSongTitle"
        android:id="@+id/songTitle"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textStyle="bold"
        android:textSize="13sp"
        android:textColor="@color/colorPrimary2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/songTitle"
        android:id="@+id/editSongTitle"
        android:layout_alignParentStart="true"
        android:editable="true"
        android:inputType="text"
        android:background="#e6e6e6"
        android:textColor="@color/colorPrimary3"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvKey"
        android:id="@+id/songKey"
        android:textStyle="bold"
        android:textSize="13sp"
        android:textColor="@color/colorPrimary2"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/editSongTitle"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editSongKey"
        android:layout_below="@+id/songKey"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editSongTitle"
        android:background="#e6e6e6"
        android:inputType="text"
        android:textColor="@color/colorPrimary3" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvTuning"
        android:id="@+id/songTuning"
        android:textStyle="bold"
        android:textSize="13sp"
        android:textColor="@color/colorPrimary2"
        android:layout_marginTop="13dp"
        android:layout_below="@+id/editSongKey"
        android:layout_alignParentStart="true" />


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editSongTuning"
        android:layout_below="@+id/songTuning"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editSongKey"
        android:background="#e6e6e6"
        android:textColor="@color/colorPrimary3"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvDuration"
        android:id="@+id/songDuration"
        android:textStyle="bold"
        android:textSize="13dp"
        android:textColor="@color/colorPrimary2"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/editSongTuning"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editSongDuration"
        android:layout_below="@+id/songDuration"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editSongTuning"
        android:background="#e6e6e6"
        android:textColor="@color/colorPrimary3"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvLyrics"
        android:id="@+id/songLyrics"
        android:textStyle="bold"
        android:textSize="13sp"
        android:textColor="@color/colorPrimary2"
        android:layout_below="@+id/editSongDuration"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15sp"
        android:textIsSelectable="false" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/editSongLyrics"
        android:layout_below="@+id/songLyrics"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editSongDuration"
        android:background="#e6e6e6"
        android:layout_above="@+id/saveButton"
        android:layout_marginBottom="20dp"
        android:textColor="@color/colorPrimary3"/>

    <Button
        android:id="@+id/saveButton"
        android:text="@string/buttonSaveText"

        android:background="@color/colorPrimary"
        android:textColor="@color/colorPrimary2"
        android:textStyle="bold"
        android:textSize="11sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="19dp"
        android:layout_marginRight="22dp"
        android:onClick="onClickSaveSong"
        android:layout_alignParentBottom="true"
         />

    <Button
        android:id="@+id/backButton"
        android:text="@string/backButtonText"
        android:background="@color/colorPrimary"
        android:textColor="@color/colorPrimary2"
        android:textStyle="bold"
        android:textSize="11sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickBack"
        android:layout_alignBottom="@+id/saveButton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="19dp"
        />

</RelativeLayout>

也许值得一提的是,ListView 本身并不位于 Activity_add_song 中,而是位于另一个 Activity activity_songs 中。那会导致空指针异常吗?

确保在 setContentView() 之后调用 populateSongList()

并且请更具体地说明第 121 行的位置。

Caused by: java.lang.NullPointerException at com.blob.gigstrofinal.AddSong.populateSongList(AddSong.java:121)

因为第 121 行是这样的:

songList.setAdapter(myCursorAdapter);

这就是你得到 null 的地方,那么 songList 或 myCursorAdapter 为 null。 myCursorAdapter 显然不为空,因此您在这一行中的 findViewById 方法的名称可能有误:

ListView songList = (ListView)findViewById(R.id.songsListView);

检查列表视图在您的 XML 中是否确实被称为 "songsListView" - 它可能不是。

问题是 ListView 不在您在此 Activity 中展开的布局 xml 中。

当你在 onCreate() 中调用它时:

setContentView(R.layout.activity_add_song);

您正在将 activity_add_song.xml 布局 xml 膨胀到当前 Window。

那么当你打电话时:

ListView songList = (ListView)findViewById(R.id.songsListView);

它正在查看 ID 为 songsListView 的 ListView 当前膨胀的布局。由于当前膨胀的布局(activity_add_song.xml)中没有 songsListView,因此它将为 null。

将处理 songsListView 的所有代码移至 Activity,用 ListView 扩充布局。

您的 activity_add_song.xml 应该包含 ListView,您的代码中缺少它。

<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" />

R.layout.songlist_layout应该包含每一行的布局

<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"/>