最近几天无聊自己动手写个音乐播放器,用到Cursor来取得数据库中音乐文件的信息,但是当用到Cursor的时候总是报空指针错误,后来发现是模拟器上没有音乐文件,使用Cursor的时候 ,若Cursor中没有结果,则Cursor的对象则为空,所以在使用Cursor的时候最好先判断Cursor对象是否为空且其结果是否大于等于1,以下是最开始的代码:

private void getSongMessage(){
String TITLE = MediaStore.Audio.Media.TITLE;
String DATA = MediaStore.Audio.Media.DATA;
String ALBUM = MediaStore.Audio.Media.ALBUM;
String ARTIST = MediaStore.Audio.Media.ARTIST;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cs = cr.query(uri, null, null, null, null); while (cs.moveToNext()) {
String title = cs.getString(cs.getColumnIndex(TITLE));
String data = cs.getString(cs.getColumnIndex(DATA));
String album = cs.getString(cs.getColumnIndex(ALBUM));
String artist = cs.getString(cs.getColumnIndex(ARTIST));
MusicData.title.add(title);
MusicData.data.add(data);
MusicData.album.add(album);
MusicData.artist.add(artist);
}
}

以下 是修改后的代码:

 private void getSongMessage(){
String TITLE = MediaStore.Audio.Media.TITLE;
String DATA = MediaStore.Audio.Media.DATA;
String ALBUM = MediaStore.Audio.Media.ALBUM;
String ARTIST = MediaStore.Audio.Media.ARTIST;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cs = cr.query(uri, null, null, null, null);
if(cs != null && cs.getCount() >= ){
while (cs.moveToNext()) {
String title = cs.getString(cs.getColumnIndex(TITLE));
String data = cs.getString(cs.getColumnIndex(DATA));
String album = cs.getString(cs.getColumnIndex(ALBUM));
String artist = cs.getString(cs.getColumnIndex(ARTIST));
MusicData.title.add(title);
MusicData.data.add(data);
MusicData.album.add(album);
MusicData.artist.add(artist);
}
}else{
Toast.makeText(this, "No Data", Toast.LENGTH_LONG).show();
}
}

Android Cursor空指针的问题的更多相关文章

  1. [Android Pro] 完美Android Cursor使用例子(Android数据库操作)

    reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...

  2. Android Cursor类的概念和用法

    http://www.2cto.com/kf/201109/103163.html 关于 Cursor 在你理解和使用 Android Cursor 的时候你必须先知道关于 Cursor 的几件事情: ...

  3. 单独谈谈 Android Cursor 的使用细节

    使用过 SQLite 数据库对 Cursor 应该不陌生,这里单独拿出来谈一下,加深对Android SQLite中使用 Cursor 的理解. 在你理解和使用 Android Cursor 的时候你 ...

  4. 如何检测 Android Cursor 泄漏

    简介: 本文介绍如何在 Android 检测 Cursor 泄漏的原理以及使用方法,还指出几种常见的出错示例.有一些泄漏在代码中难以察觉,但程序长时间运行后必然会出现异常.同时该方法同样适合于其他需要 ...

  5. Android Cursor浅析

    1. 本文目的 Android ContentProvider提供了进程间数据交换的一种机制.而数据库的查询就是这样的机制的应用.那么app通过Uri查询数据库而得到的Cursor到底是个什么东西?为 ...

  6. Android笔记——关于Cursor类的介绍

    使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android 中使用 Cursor 的理解. 关于 Cursor 在你理解和使用 Android Cursor 的时候你 ...

  7. Android中Cursor(游标)类的概念和用法

    使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...

  8. android中的Cursor类

    转载: 使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿 ...

  9. Android中Cursor类的概念和用法

    http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...

随机推荐

  1. linux文件的通用操作方法学习

    2014-07-29 23:36:10 在linux下用文件描述符来表示设备文件和普通文件.文件描述符是一个整型的数据,所有对文件的操作都通过文件描述符实现. 文件描述符示文件系统中连接用户空间和内核 ...

  2. android连接本地tomcat服务器,报timeout

    1.在eclipse环境下连接时,没有任何问题 2.直接将服务端发布到tomcat服务下,报timeout 3.查明原因: 3.1打开IE访问,一切正常,可以获取到数据,说明不是服务端的问题 3.2打 ...

  3. MVC中的几个问题汇总

    1.The model backing the 'XXXXDBContext' context has changed since the database was created. Either m ...

  4. vagrant在windows下的使用

    vagrant在windows下的使用 下载安装 VirtualBox :https://www.virtualbox.org/ 下载安装 Vagrant :http://www.vagrantup. ...

  5. Android crop image size

    private void performCrop() { try { //call the standard crop action intent (the user device may not s ...

  6. bw R/3端配置 (转)

    先检查一下两边系统的补丁:R3端如下, BW端按照老师的说法至少补丁要打到17,貌似我们是19,通过,这样做起事情来后顾无忧 登陆R3界面,SBIW这个是R3的最常用事务码,有关BW的所有东东都在他的 ...

  7. python学习小结4:类

    虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 类和对象是面向对象编程的两个主要方面.类:创建一个新类型,而对象是这个类的实例,类使用class关键字创建.类的域和方法被列在一个 ...

  8. Java 8 VM GC Tuning Guide Charter3-4

    第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...

  9. linux下MySQL 5.6源码安装

    linux下MySQL 5.6源码安装 1.下载:当前mysql版本到了5.6.20 http://dev.mysql.com/downloads/mysql 选择Source Code 2.必要软件 ...

  10. Thinkphp中路由Url获取的使用方法

    Thinkphp是一个体系较为完整的框架,很多地方比国外的框架功能都全,唯一不喜之处是性能,和传说中的.NET有点像. Thinkphp提供较全url处理体系,通过同一规则实现Url的路由和Url生成 ...