Android开发 简易音乐播放器列表无法显示

Android编程,设计一个简易的音乐播放器
主要是通过 ContentResolver 查询 MediaStore 多媒体文件获取文件名,列表显示的。目前真机调试是正常的,可以正常显示列表。
但是在模拟机上调试,列表显示是空白。logcat提示如下截图:
图片说明

MainActivity.java中代码如下:

package com.example.hp.ggmusic;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.MediaController;

public class MainActivity extends AppCompatActivity {

    private final int REQUEST_EXTERNAL_STORAGE= 1;
    private static String[] PERMISSIONS_STORAGE={
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private ContentResolver mContentResolver;
    private ListView mPlaylist;
    private MediaCursorAdapter mCursorAdapter;
    //private Cursor mCursor;

    //音频文件是否属于音乐类型
    private final String SELECTION =
            MediaStore.Audio.Media.IS_MUSIC + " = ?" + " AND " +
                    MediaStore.Audio.Media.MIME_TYPE + " LIKE ?";
    //音频文件的MIME类型,MP3对应的是“audio/mpeg”
    private final String[] SELECTION_ARGS ={
            Integer.toString(1),
            "audio/mpeg"};

    //
    @RequiresApi(api = Build.VERSION_CODES.M)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContentResolver=getContentResolver(); //获取本地资源文件列表
        mCursorAdapter=new MediaCursorAdapter(MainActivity.this);
        mPlaylist=findViewById(R.id.lv_playlist);
        mPlaylist.setAdapter(mCursorAdapter);

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                !=PackageManager.PERMISSION_GRANTED) {
            if(ActivityCompat.shouldShowRequestPermissionRationale(
                    MainActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)){
            }else {
                requestPermissions(PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        }else {
            initPlaylist();
        }
    }
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions,int[] grantResults){
        switch (requestCode){
            case REQUEST_EXTERNAL_STORAGE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    initPlaylist();
                }
                break;
            default:
                break;
        }
    }

    private void initPlaylist(){
        //查询本地资源
        Cursor mCursor=mContentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null,
                SELECTION,
                SELECTION_ARGS,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER
        );
        mCursorAdapter.swapCursor(mCursor);
        mCursorAdapter.notifyDataSetChanged();
    }

}

编写的适配器MediaCursorAdapter.java中的代码如下

package com.example.hp.ggmusic;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class MediaCursorAdapter extends CursorAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MediaCursorAdapter(Context context){
        super(context,null,0);
        mContext=context;

        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public View newView(Context context,
                        Cursor cursor, ViewGroup viewGroup){
        View itemView =mLayoutInflater.inflate(R.layout.list_item,
                viewGroup,false);
        if (itemView != null){
            ViewHolder vh = new ViewHolder();
            vh.tvTitle = itemView.findViewById(R.id.tv_title);
            vh.tvArtist=itemView.findViewById(R.id.tv_artist);
            vh.tvOrder=itemView.findViewById(R.id.tv_order);
            vh.divider=itemView.findViewById(R.id.divider);
            itemView.setTag(vh);

            return itemView;
        }

        return null;
    }

    public  class  ViewHolder{
        TextView tvTitle;
        TextView tvArtist;
        TextView tvOrder;
        View divider;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder vh=(ViewHolder) view.getTag();

        int titleIndex=cursor.getColumnIndex(
                MediaStore.Audio.Media.TITLE);
        int artistIndex=cursor.getColumnIndex(
                MediaStore.Audio.Media.ARTIST);

        String title =cursor.getString(titleIndex);
        String artist =cursor.getString(artistIndex);

        int position=cursor.getPosition();

        if (vh!=null){
            vh.tvTitle.setText(title);
            vh.tvArtist.setText(artist);
            vh.tvOrder.setText(Integer.toString(position + 1));
        }
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问