有两个错误,我都无法弄清楚为什么。无法解析方法“ getContentResolver()”2.无法推断参数(在new ArrayAdapter<>()上)。我希望这是从fragment获取歌曲列表的正确方法吗?如果有更好的选择,请解释一下。谢谢public class SongsFragment extends Fragment { public SongsFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_songs, container, false); ListView audioView = (ListView)view.findViewById(R.id.songView); ArrayList<String> audioList = new ArrayList<>(); String[] proj = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME}; final Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null); if (audioCursor != null) { if (audioCursor.moveToFirst()) { do { int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); audioList.add(audioCursor.getString(audioIndex)); } while (audioCursor.moveToNext()); } } audioCursor.close(); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, audioList); audioView.setAdapter(adapter); return view; }} 最佳答案 在扩展Fragment的类中,无法使用上下文,需要使用getActivity()方法。1.无法解析方法“ getContentResolver()”因为getContentResolver方法在Fragment类中不可用,所以需要用户Context来访问它: final Cursor audioCursor = getActivity().getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);2.无法推断参数(在新的ArrayAdapter ()上)。因为ArrayAdapter构造函数需要有效的Context作为第一个参数:ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, audioList); 08-18 13:14