编写音乐播放器的时候在写获取SQLite里面的本地音乐清单时遇到如下这个异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.activity.Inventory}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
下面是我的获取SQLite里面的本地音乐清单的代码类
public class Locality extends Activity {
public List> getCatalog() {
Cursor mAudioCursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
null, // 查询条件
null, // 条件的对应?的参数
MediaStore.Audio.AudioColumns.TITLE);// 排序方式
List> mListData = new ArrayList>();
for (int i = 0; i < mAudioCursor.getCount(); i++) {
mAudioCursor.moveToNext();
// 找到歌曲标题和总时间对应的列索引
int indexTitle = mAudioCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);// 歌名
int indexARTIST = mAudioCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);// 艺术家
/*
- int indexALBUM = mAudioCursor
- .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);//专辑 / String strTitle = mAudioCursor.getString(indexTitle); String strARTIST = mAudioCursor.getString(indexARTIST); / String strALBUM = mAudioCursor.getString(indexALBUM); */
HashMap nowMap = new HashMap();
nowMap.put("SongName", strTitle);
nowMap.put("SongMessage", strARTIST);
mListData.add(nowMap);
}
return mListData;
}
}
提示我是这一行出错:
Cursor mAudioCursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,// 字段 没有字段 就是查询所有信息 相当于SQL语句中的 “
// * ”
null, // 查询条件
null, // 条件的对应?的参数
MediaStore.Audio.AudioColumns.TITLE);// 排序方式