我一直在开发音乐播放器应用程序,但每次到达列表视图末尾并且我不知道如何解决它时,它都会崩溃。
logcat向我显示了java.lang.NumberFormatException:空错误,我尝试更改该值,但是没有用。
这是我的代码:
public class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c, new String[]{MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.MediaColumns.TITLE,MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.displayname, R.id.title, R.id.duration});
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
name.setText(cursor.getString(cursor.getColumnIndex(
MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(cursor.getColumnIndex(
MediaStore.MediaColumns.TITLE)));
long durationInMS = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
double durationInMin = ((double) durationInMS / 1000.0) / 60.0;
durationInMin = new BigDecimal(Double.toString(durationInMin)).
setScale(2, BigDecimal.ROUND_UP).doubleValue();
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item, parent, false);
bindView(v, context, cursor);
return v;
}
}
这是logcat中显示的部分:
long durationInMS = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
logcat:
2019-03-23 09:58:12.979 31713-31713/com.example.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.musicplayer, PID: 31713
java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:553)
at java.lang.Long.parseLong(Long.java:632)
at com.example.musicplayer.MediaCursorAdapter.bindView(MediaCursorAdapter.java:35)
at android.widget.CursorAdapter.getView(CursorAdapter.java:289)
at android.widget.AbsListView.obtainView(AbsListView.java:2428)
at android.widget.ListView.makeAndAddView(ListView.java:2083)
at android.widget.ListView.fillDown(ListView.java:793)
at android.widget.ListView.fillGap(ListView.java:757)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5520)
at android.widget.ListView.trackMotionScroll(ListView.java:1991)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5052)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:718)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
答案 0 :(得分:1)
尝试一下:
duration.setText("" + String.valuOf(durationInMin));
而不是:
duration.setText("" + durationInMin);
答案 1 :(得分:1)
您应该获得尽可能长的值,
long durationInMS = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION));
然后将值设置为
duration.setText(String.valueOf(durationInMS));
答案 2 :(得分:0)
您需要检查durationInMS
是可以解析的数字:
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)
如果不是数字,可能是个问题。
如果不是数字,则可以使用String.valuOf
这样显示文本:
duration.setText(String.valuOf(durationInMin)+ "" );