1

我一直在开发一个音乐播放器应用程序,但每次我到达列表视图的末尾时它都会崩溃,我不知道如何修复它。

logcat 向我展示了 java.lang.NumberFormatException: null 错误,我尝试更改该值,但没有成功。

这是我的代码:


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)));

日志猫:

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)
4

3 回答 3

1

你应该得到这个值,

long durationInMS =   cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION));

然后将值设置为,

 duration.setText(String.valueOf(durationInMS));
于 2019-03-23T09:41:41.247 回答
1

尝试这个 :

duration.setText("" + String.valuOf(durationInMin));

代替 :

duration.setText("" + durationInMin);
于 2019-03-23T09:25:04.630 回答
0

您需要检查这durationInMS是一个可以解析的数字:
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION) 如果它不是数字,则可能是问题所在。

如果它不是一个数字,你可以使用String.valuOf这样显示你的文本:

duration.setText(String.valuOf(durationInMin)+ "" );
于 2019-03-23T09:26:28.473 回答