我正在tabLayout中创建一个列表视图。当我运行应用程序时,它说没有适配器附加,跳过布局,我无法在模拟器中看到列表;它显得空白。请帮我。我几乎尝试了互联网上的每一个答案,但却无法做到。我可以看到除了屏幕上的所有其他东西。
E / RecyclerView:没有连接适配器;跳过布局。 01-02 05:53:38.841 19262-19262 / com.example.murarilal.musicmania E / RecyclerView:没有连接适配器;跳过布局01-02 05:53:38.842 19262-19262 / com.example.murarilal.musicmania I / Choreographer:跳过32帧!该应用程序可能也在做 在其主要线程上做了很多工作。 01-02 05:53:38.923 19262-19278 / com.example.murarilal.musicmania E / EGL_emulation:tid 19278:eglSurfaceAttrib(1227):错误0x3009(EGL_BAD_MATCH)01-02 05:53:38.923 19262-19278 / com.example.murarilal.musicmania W / OpenGLRenderer:无法在曲面上设置EGL_SWAP_BEHAVIOR 0x7632b4a689c0,错误= EGL_BAD_MATCH 01-02 05:53:38.939 19262-19278 / com.example.murarilal.musicmania D / EGL_emulation: eglMakeCurrent:0x7632b4a493c0:ver 2 0(tinfo 0x7632b4a0c6a0)
主要活动:
public class SongsTab extends Fragment {
private ArrayList<songInfo> songs = new ArrayList<songInfo>();
RecyclerView recyclerView;
SeekBar seekBar;
songAdapter songAdapter1;
MediaPlayer mediaPlayer;
private Handler myHandler = new Handler();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.songs, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.recyclerView);
seekBar = (SeekBar)v.findViewById(R.id.seekBar);
songAdapter1 = new songAdapter(songs, getActivity());
recyclerView.setAdapter(songAdapter1);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
songAdapter1.setOnitemClickListener(new songAdapter.OnitemClickListener() {
@Override
public void onItemclick(songAdapter.ViewHolder holder, View v, final songInfo obj, int position) {
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(obj.getSongPath());
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 v;
}
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 cursor = getContext().getContentResolver().query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String SongPath = cursor.getString(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());
}
}
}
Adapter
public class songAdapter extends RecyclerView.Adapter<songAdapter.ViewHolder> {
ArrayList<songInfo> songs;
Context context;
OnitemClickListener onitemClickListener;
public songAdapter(ArrayList<songInfo> songs, Context context) {
this.songs = songs;
this.context = context;
}
public interface OnitemClickListener{
void onItemclick(ViewHolder holder, View v, songInfo obj, int position);
}
public void setOnitemClickListener(OnitemClickListener onitemClickListener)
{
this.onitemClickListener=onitemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView= LayoutInflater.from(context).inflate(R.layout.card,parent,false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final songInfo song = songs.get(position);
holder.songName.setText(songs.get(position).getSongName());
holder.artistName.setText(songs.get(position).getArtistName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onitemClickListener!=null)
{
onitemClickListener.onItemclick(holder,view,song,position);
}
}
});
}
@Override
public int getItemCount() {
return songs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView songName,artistName;
public ViewHolder(View itemView) {
super(itemView);
songName=(TextView)itemView.findViewById(R.id.songName);
artistName=(TextView)itemView.findViewById(R.id.artistName);
}
}
}
答案 0 :(得分:0)
recyclerView.setAdapter(songAdapter1);
在此行之后添加上一行
songAdapter1 = new songAdapter(songs, getActivity());
loadSongs()
方法中的
答案 1 :(得分:0)
我认为这篇文章可以帮到你。检查您的代码并尝试正确设置layoutmanager