RecyclerView没有连接适配器; 跳过布局,数据不显示
我正在制作一个音乐播放器应用程序,我正在使用加载器将歌曲数据加载到适配器,该适配器将使用RecyclerView显示。 但是,我得到这个奇怪的错误,我的适配器方法无法正常工作。 只调用适配器的构造函数方法。 尽管在堆栈溢出中通过所有可用的解决方案,我也得到“没有适配器连接;跳过布局”。
几点需要注意:
- 我已经在recyclerview中尝试了所有解决方案“无适配器连接;跳过布局” 没有连接适配器; 跳过布局线程和所有关联的重复线程。
- 我使用的RecyclerView不是常规的,而是FastScrollRecyclerView ,但由于它从常规的RecyclerView扩展而且github上没有提到相关的问题,所以我确信在这里使用这个库不是问题
- 我也尝试过没有从这个线程调用适配器方法的所有解决方案,但没有运气。
这是代码:
SongsFragment.java
public class SongsFragment extends Fragment implements LoaderManager.LoaderCallbacks<List>{ public static final String LOG_TAG = SongsFragment.class.getSimpleName(); private static final int LOADER_ID = 1; private ContentResolver mContentResolver; private SongListAdapter mSongListAdapter; private List mSongs; @BindView(R.id.rvSongs) FastScrollRecyclerView mRecyclerView; public SongsFragment() {} @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ButterKnife.bind(getActivity()); mSongs = new ArrayList(); mRecyclerView = new FastScrollRecyclerView(getContext()); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setHasFixedSize(true); mSongListAdapter = new SongListAdapter(getContext(), mSongs); mRecyclerView.setAdapter(mSongListAdapter); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getLoaderManager().initLoader(LOADER_ID, null, this).forceLoad(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_songs, container, false); } @Override public Loader<List> onCreateLoader(int id, Bundle args) { mContentResolver = getActivity().getContentResolver(); return new SongsLoader(getContext(), mContentResolver); } @Override public void onLoadFinished(Loader<List> loader, List data) { mSongs = data; mSongListAdapter.setData(mSongs); } @Override public void onLoaderReset(Loader<List> loader) { mSongListAdapter.setData(new ArrayList()); }
}
SongsListAdapter.java
public class SongListAdapter extends FastScrollRecyclerView.Adapter implements FastScrollRecyclerView.SectionedAdapter{ public static final String LOG_TAG = SongListAdapter.class.getSimpleName(); private Context mContext; private List mSongList = new ArrayList(); public SongListAdapter(Context context, List songList) { Log.d(LOG_TAG, "Constructor called"); mContext = context; mSongList = songList; } @NonNull @Override public String getSectionName(int position) { return String.valueOf(mSongList.get(position).getTitle().charAt(0)).toUpperCase(); } @Override public SongItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Log.d(LOG_TAG, "onCreateViewHolder called"); View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_song, null); return new SongItemViewHolder(view); } @Override public void onBindViewHolder(SongItemViewHolder holder, int position) { Log.d(LOG_TAG, "onBindViewHolder called"); Uri albumArtUri = mSongList.get(position).getAlbumArtUri(); Glide.with(mContext) .load(albumArtUri) .into(holder.albumArt); holder.titleText.setText(mSongList.get(position).getTitle()); holder.artistText.setText(mSongList.get(position).getArtistName()); Log.d("Data", albumArtUri.toString() + "\n" + mSongList.get(position).getTitle() + "\n" + mSongList.get(position).getArtistName()); } @Override public int getItemCount() { Log.d(LOG_TAG, "getItemCount called"); return (mSongList != null ? mSongList.size() : 0); } public void setData(List songs){ mSongList = songs; notifyDataSetChanged(); } public class SongItemViewHolder extends FastScrollRecyclerView.ViewHolder { ImageView albumArt; TextView titleText; TextView artistText; SongItemViewHolder(View view) { super(view); Log.d(LOG_TAG, "SongItemViewHolder called"); albumArt = (ImageView) view.findViewById(R.id.item_song_image); titleText = (TextView) view.findViewById(R.id.item_song_title); artistText = (TextView) view.findViewById(R.id.item_song_artist_name); } }
}
fragment_songs.xml(SongsFragment正在膨胀此布局)
list_item_song.xml(回收站视图中的单个项目)
这真是令人沮丧。 请查看代码并帮助我。 我想我已经做好了一切,但我可能错了。 我知道scrollview和recyclerview不是那么顺利,但我已经看过预览和回收者视图显示。 任何帮助将不胜感激。 谢谢!
尝试在onLoadFinished()
设置适配器,并在适配器对象中使用getActivity()
作为上下文
@Override public void onLoadFinished(Loader> loader, List data) { mSongs = data; mSongListAdapter = new SongListAdapter(getActivity(), mSongs); mRecyclerView.setAdapter(mSongListAdapter); }
也在这个mRecyclerView = new FastScrollRecyclerView(getContext());
至
mRecyclerView = new FastScrollRecyclerView(getActivity());
基本上在片段类中使用getActivity()
作为上下文
啊! 我浪费了很多时间,但终于拿出了解决方案。 我删除了Butterknife绑定,并在通过将onCreateView()
更改SongsFragment
捕获视图实例后,在SongsFragment
onCreateView()
中使用了常规的findViewById
:
@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_songs, container, false); mRecyclerView = (FastScrollRecyclerView) rootView.findViewById(R.id.rvSongs); //Rest of the things }
事实certificate我使用ButterKnife的方式是错误的,因此实例mRecyclerView
为null但后来使用了行mRecyclerView = new FastScrollRecyclerView(getContext());
它不再是null但它仍然没有连接到视图所以我没有得到NullPointerException
并且代码不起作用。
我知道这是一个菜鸟错误:D
从官方网站上获取的使用ButterKnife片段的正确方法是:
public class FancyFragment extends Fragment { @BindView(R.id.button1) Button button1; @BindView(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } }
- 如何通过蓝牙查询远程手机是否支持PBAP?
- 仅针对Android Marshmallow请求运行时权限?
- Android RenderScript FinalizerWatchdogDaemon致命exception
- java.lang.ArrayIndexOutOfBoundsException:length = 4; index = 4长度数组
- Android TextView上标
- 将数据传递给添加的片段 – IlligalStateException:片段已处于活动状态
- 关于在android中使用Radix Tree在240k单词列表中进行英语词典单词查询的问题
- 如何使用phonegap插件从android调用javascript函数
- 什么是好的机器人拆解可以产生信息效果