当前位置: 首页 > 知识库问答 >
问题:

没有连接适配器;跳过版式[重复]

漆雕嘉平
2023-03-14

logcat错误:没有连接适配器;跳过布局

我用getActivity()更改了上下文参数,但应用程序仍然没有运行。

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);
    }
}

共有1个答案

拓拔弘化
2023-03-14

您没有附加适配器,因为您是在尝试附加适配器之后创建它的:

mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
 类似资料: