开发者社区> 问答> 正文

Android获取SQLite里面的本地音乐清单

编写音乐播放器的时候在写获取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);// 排序方式

展开
收起
a123456678 2016-07-18 16:21:17 2823 0
1 条回答
写回答
取消 提交回答
  • Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

    空指针啊

    2019-07-17 19:58:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载