Android获取SQLite里面的本地音乐清单
编写音乐播放器的时候在写获取SQLite里面的本地音乐清单时遇到如下这个异常:java.lang.RuntimeException:Unabletostartactivi...
编写音乐播放器的时候在写获取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<Map<String, Object>> getCatalog() {
Cursor mAudioCursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
null, // 查询条件
null, // 条件的对应?的参数
MediaStore.Audio.AudioColumns.TITLE);// 排序方式
List<Map<String, Object>> mListData = new ArrayList<Map<String, Object>>();
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<String, Object> nowMap = new HashMap<String, Object>();
nowMap.put("SongName", strTitle);
nowMap.put("SongMessage", strARTIST);
mListData.add(nowMap);
}
return mListData;
}
}
提示我是这一行出错:Cursor mAudioCursor = this.getContentResolver().query( 展开
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<Map<String, Object>> getCatalog() {
Cursor mAudioCursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
null, // 查询条件
null, // 条件的对应?的参数
MediaStore.Audio.AudioColumns.TITLE);// 排序方式
List<Map<String, Object>> mListData = new ArrayList<Map<String, Object>>();
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<String, Object> nowMap = new HashMap<String, Object>();
nowMap.put("SongName", strTitle);
nowMap.put("SongMessage", strARTIST);
mListData.add(nowMap);
}
return mListData;
}
}
提示我是这一行出错:Cursor mAudioCursor = this.getContentResolver().query( 展开
展开全部
Locality在AndroidMainfest.xml中申明了吗?
oncreate方法呢?
布局设置了吗?
oncreate方法呢?
布局设置了吗?
追问
.。。。。。。。。。。。。。。当然,我想问的是代码问题,别的我都知道,亲~~~麻烦先看下代码
追答
android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
这里说得很清楚了 你的this.getContentResolver()是一个空对象引用。那肯定是你context的问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询