recyclerView 没有适配器附加,跳过布局

Posted

技术标签:

【中文标题】recyclerView 没有适配器附加,跳过布局【英文标题】:recyclerView no adapter attach,skipping layout 【发布时间】:2018-06-12 08:17:20 【问题描述】:

我正在 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: 版本 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);

        

    

【问题讨论】:

您是如何找到解决方案的? 但是,我没有得到解决方案。 我不知道这有什么问题 【参考方案1】:
 recyclerView.setAdapter(songAdapter1);

在此行之后添加上面的行 songAdapter1 = new songAdapter(songs, getActivity());

在您的 loadSongs() 方法中

【讨论】:

【参考方案2】:

我认为这篇文章可以帮助你。检查您的代码并尝试正确设置布局管理器

USING RECYCLERVIEW

【讨论】:

只需复制 logcat 并发布 如果错误仍然显示为recyclerView no adapter attach, skipping layout。尝试修复回收站视图的 LayoutManager

以上是关于recyclerView 没有适配器附加,跳过布局的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView:没有附加适配器;跳过布局[重复]

Firebase:Recyclerview 没有附加适配器,跳过布局

Firebase:Recyclerview 没有附加适配器,跳过布局

E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局

RecyclerView:没有附加适配器;跳过布局错误

Firebase:RecyclerView 跳过布局没有附加适配器