我收到了错误
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception 'java.lang.IllegalStateException' thrown in void android.os.MessageQueue.nativePollOnce(long, int):-2
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] in call to CallVoidMethodV
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] from void android.os.MessageQueue.nativePollOnce(long, int)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] "main" prio=5 tid=1 Runnable
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] | group="main" sCount=0 dsCount=0 obj=0x75b64fb8 self=0x55b4a5c090
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] | sysTid=9364 nice=0 cgrp=default sched=0/0 handle=0x7facc47e80
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] | state=R schedstat=( 1507395371 53367917 863 ) utm=138 stm=12 core=4 HZ=100
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] | stack=0x7fe3fcd000-0x7fe3fcf000 stackSize=8MB
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] | held mutexes= "mutator lock"(shared held)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] native: #00 pc 000039a4 /system/lib64/libbacktrace_libc++.so (_ZN9Backtrace6UnwindEmP8ucontext+28)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] native: #01 pc 0000001c ???
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at android.os.MessageQueue.nativePollOnce(Native method)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at android.os.MessageQueue.next(MessageQueue.java:143)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at android.os.Looper.loop(Looper.java:122)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at android.app.ActivityThread.main(ActivityThread.java:5276)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at java.lang.reflect.Method.invoke!(Native method)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at java.lang.reflect.Method.invoke(Method.java:372)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
09-10 10:50:32.770 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/check_jni.cc:65]
09-10 10:50:33.070 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/runtime.cc:289] Runtime aborting...
09-10 10:50:33.070 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/runtime.cc:289] Aborting thread:
09-10 10:50:33.070 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/runtime.cc:289] "main" prio=5 tid=1 Native
09-10 10:50:33.070 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/runtime.cc:289] | group="" sCount=0 dsCount=0 obj=0x75b64fb8 self=0x55b4a5c090
09-10 10:50:33.070 9364-9364/com.sprdh.asmaulhusna A/art: art/runtime/runtime.cc:289] | sysTid=9364 nice=0 cgrp=default sched=0/0 handle=0x7facc47e80
完整logcat logcat
使用Recyclerview列出我手机中的所有歌曲
public class SongsFragment extends Fragment {
private static String TAG = SongsFragment.class.getSimpleName();
private List<SongModel> songList = new ArrayList<>();
private SongsListAdapter mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_songs_fragment_main, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
// getting song list
// if (songList != null) {
// songList.clear();
//
// }
setupSongList(recyclerView);
return view;
}
private void setupSongList(RecyclerView recyclerView) {
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " COLLATE LOCALIZED ASC";
Cursor musicCursor = musicResolver.query(musicUri, null, selection, null, sortOrder);
if (musicCursor != null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int albumColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM);
int albumIdColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM_ID);
int durationColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.DURATION);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
long albumId = musicCursor.getLong(albumIdColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisALbum = musicCursor.getString(albumColumn);
int thisDuration = musicCursor.getInt(durationColumn);
songList.add(new SongModel(thisId, thisTitle, thisArtist, thisALbum, albumId, thisDuration));
}
while (musicCursor.moveToNext());
}
// songList.add(new SongModel(1, "thisTitle1", "thisArtist1", "thisALbum", 1, 1));
// songList.add(new SongModel(2, "thisTitle2", "thisArtist2", "thisALbum", 2, 2));
mAdapter = new SongsListAdapter(getActivity(), songList);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
} }
as Fragment和
public class SongsListAdapter extends RecyclerView.Adapter<SongsListAdapter.AllSongsGridHolder> {
private static String TAG = SongsListAdapter.class.getSimpleName();
private List<SongModel> allSongsList;
private Context mContext;
public SongsListAdapter(Activity context, List<SongModel> allSongsList) {
this.allSongsList = allSongsList;
this.mContext = context;
}
@Override
public AllSongsGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_row_item_song, parent, false);
return new AllSongsGridHolder(v);
}
@Override
public void onBindViewHolder(AllSongsGridHolder holder, int position) {
SongModel model = allSongsList.get(position);
holder.artist.setText(model.getArtist());
holder.title.setText(model.getTitle());
}
@Override
public int getItemCount() {
return allSongsList.size();
}
public class AllSongsGridHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView title, artist;
protected ImageView albumArt;
public AllSongsGridHolder(View itemView) {
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.song_title);
this.artist = (TextView) itemView.findViewById(R.id.song_artist);
this.albumArt = (ImageView) itemView.findViewById(R.id.albumArt);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
} }
和
public class SongModel {
private long songId;
private String title;
private String artist;
private String album;
private long albumId;
private int duration;
private int year;
public SongModel(long songID, String songTitle, String songArtist, String songAlbum, long songalbumId, int songDuration) {
this.songId = songID;
this.title = songTitle;
this.artist = songArtist;
this.album = songAlbum;
this.albumId = songalbumId;
this.duration = songDuration;
}
public long getSongId() {
return songId;
}
public void setSongId(long songId) {
this.songId = songId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public long getAlbumId() {
return albumId;
}
public void setAlbumId(long albumId) {
this.albumId = albumId;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
如果我使用它,则没有错误
//for test
songList.add(new SongModel(1, "thisTitle1", "thisArtist1", "thisALbum", 1, 1));
songList.add(new SongModel(2, "thisTitle2", "thisArtist2", "thisALbum", 2, 2));
任何人都可以建议这是什么?或者更好的方法来做到这一点