问题
I am getting an error saying no adapter attach skipping layout.and the list doesn't appear in when emulator runs.please help.I cant see anything in list.screen appears blank.other items in the activity are appearing.i have seen almost all answer on the internet but didn't get any solution
this is the logcat error. E/RecyclerView: No adapter attached; skipping layout
public class SongsTab extends Fragment {
private ArrayList<songInfo> _songs = new ArrayList<>();
;
RecyclerView recyclerView;
SeekBar seekBar;
songAdapter songAdapter1;
MediaPlayer mediaPlayer;
private Handler myHandler = new Handler();
Cursor cursor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.songs, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
seekBar = v.findViewById(R.id.seekBar);
songAdapter1 = new songAdapter(_songs, getContext());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(songAdapter1);
songAdapter1.setOnitemClickListener(new songAdapter.OnitemClickListener() {
@Override
public void onItemclick(songAdapter.ViewHolder holder, View v, songInfo obj, int position) {
System.gc();
int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
cursor.moveToPosition(position);
final String filename = cursor.getString(songPath);
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
} else {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filename);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mediaPlayer.getDuration());
Log.d("Prog", "run: " + mediaPlayer.getDuration());
}
});
} catch (Exception e) {
}
}
};
myHandler.postDelayed(runnable, 100);
}
}
});
checkUserPermission();
Thread t = new runThread();
t.start();
return null;
}
public class runThread extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("Runwa", "run: " + 1);
if (mediaPlayer != null) {
seekBar.post(new Runnable() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
});
Log.d("Runwa", "run: " + mediaPlayer.getCurrentPosition());
}
}
}
}
private void checkUserPermission(){
if(Build.VERSION.SDK_INT>=23){
if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},123);
return;
}
}
loadSongs();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 123:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
loadSongs();
}else{
Toast.makeText(getContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
checkUserPermission();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void loadSongs(){
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
cursor =getActivity().getContentResolver().query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
songInfo s = new songInfo(name,artist,songPath);
_songs.add(s);
}while (cursor.moveToNext());
}
cursor.close();
songAdapter1 = new songAdapter(_songs,getActivity());
}
}
}
回答1:
Because Your _songs ArrayList
is Empty
.
回答2:
Try this
private void loadSongs(){
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
cursor =getActivity().getContentResolver().query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
songInfo s = new songInfo(name,artist,songPath);
_songs.add(s);
songAdapter1 = new songAdapter(_songs,getActivity());
recyclerView.setAdapter(songAdapter1);
}while (cursor.moveToNext());
}
cursor.close();
}
}
回答3:
In loadSongs, you create a new adapter instance, which is not in any way connected with your RecyclerView. Instead call
songAdapter1.notifyDatasetChanged();
after you added all items to your _songs List
来源:https://stackoverflow.com/questions/48057595/no-adapter-attach-skipping-layout