logcat错误:没有连接适配器;跳过布局
我使用getActivity()更改了context参数,但应用程序仍未运行。
public class FragmentActivity extends Fragment {
private RecyclerView mRecyclerView;
private CountryAdapter mAdapter;
private LinearLayoutManager layoutManager;
public FragmentActivity(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
答案 0 :(得分:15)
您没有附加适配器,因为您在尝试附加适配器后创建了适配器:
mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
答案 1 :(得分:4)
在我的情况下出现此问题是因为我的布局上有一个视图移动了我的列表容器并显示太小