我正在使用androidhive.info和SongsManager.java中的音频播放器教程我尝试将持续时间转换为分钟和秒。我设法做到了,但我不断得到NumberFormatException
,我不知道如何处理。部分代码在这里:
public ArrayList<HashMap<String, String>> getPlayList(Context c) {
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
/*use content provider to get beginning of database query that queries for all audio by display name, path
and mimtype which i dont use but got it incase you want to scan for mp3 files only you can compare with RFC mimetype for mp3's
*/
final Cursor mCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.MIME_TYPE,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.DURATION }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
String songs_name = "";
String mAudioPath = "";
String songs_artist = "";
String songs_album = "";
String songs_duration = "";
/* run through all the columns we got back and save the data we need into the arraylist for our listview*/
if (mCursor.moveToFirst()) {
do {
String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
songs_artist = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
songs_album = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
songs_duration = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
HashMap<String, String> song = new HashMap<String, String>();
songs_duration = setCorrectDuration(songs_duration);
song.put("songTitle", songs_name);
song.put("songPath", mAudioPath);
song.put("songArtist", songs_artist);
song.put("songAlbum", songs_album);
song.put("songDuration", songs_duration);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close(); //cursor has been consumed so close it
return songsList;
}
private String setCorrectDuration(String songs_duration) {
// TODO Auto-generated method stub
if(Integer.valueOf(songs_duration) != null) {
int time = Integer.valueOf(songs_duration);
int seconds = time/1000;
int minutes = seconds/60;
seconds = seconds % 60;
if(seconds<10) {
songs_duration = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
} else {
songs_duration = String.valueOf(minutes) + ":" + String.valueOf(seconds);
}
return songs_duration;
}
return null;
}
这是LogCat的一部分:
10-28 23:39:13.047:W / dalvikvm(30524):threadid = 1:线程退出 未捕获的异常(组= 0x422f2700)10-28 23:39:13.047: E / AndroidRuntime(30524):致命异常:主10-28 23:39:13.047: E / AndroidRuntime(30524):java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.vany.vansmusic / com.vany.vansmusic.AndroidBuildingMusicPlayerActivity}: java.lang.NumberFormatException:无效的int:&#34; null&#34;
10-28 23:39:13.047:E / AndroidRuntime(30524):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 10-28 23:39:13.047:E / AndroidRuntime(30524):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 10-28 23:39:13.047:E / AndroidRuntime(30524):at android.app.ActivityThread.access $ 700(ActivityThread.java:165)10-28 23:39:13.047:E / AndroidRuntime(30524):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1326)
我真的很感激帮助。
更新 找出任何有相同或类似问题的人:
if (mCursor.moveToFirst()) {
do {
String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
songs_artist = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
songs_album = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
songs_duration = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
HashMap<String, String> song = new HashMap<String, String>();
if(String.valueOf(songs_duration) != null) {
try {
Long time = Long.valueOf(songs_duration);
long seconds = time/1000;
long minutes = seconds/60;
seconds = seconds % 60;
if(seconds<10) {
String csongs_duration = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
song.put("songDuration", csongs_duration);
} else {
String ccsongs_duration = String.valueOf(minutes) + ":" + String.valueOf(seconds);
song.put("songDuration", ccsongs_duration);
}
} catch(NumberFormatException e) {
song.put("songDuration", songs_duration);
}
} else {
String nothing = "0";
song.put("songDuration", nothing);
}
song.put("songTitle", songs_name);
song.put("songPath", mAudioPath);
song.put("songArtist", songs_artist);
song.put("songAlbum", songs_album);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close(); //cursor has been consumed so close it
return songsList;
答案 0 :(得分:0)
http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html 您需要检查查询的返回类型。 DURATION字段不返回String,返回long。
String songs_duration = "";
应该是
long songs_duration = "";
然后您将不必解析或使用空检查。除此之外,你的数学和一切都会结束。