我是使用recycviewview的新手,将它与cardview一起使用,有人可以告诉我如何初始化它吗?
我看到我必须以某种方式将它放在onCreateView中,而不是在ViewCreated中,我的代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_genres, container, false);
和回收者的代码我在Activity上使用的是什么,但现在我想在片段中触发它
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
数据痕迹:
JSON>get>DataModel>Adapter>Recycler>CardView
当我在onCreatedView中使用它时,LogCat说:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
答案 0 :(得分:7)
就像这样
let z: (Int, String) = largestNum(interestingNumbers)
答案 1 :(得分:1)
试试这个
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_genres, container, false);
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager);
return v;
答案 2 :(得分:0)
在这里,我成功地将Recyclerview集成到一个名为MainFragment的片段中 item click for recycler view 主活动中片段的初始化不存在。用它来引用片段
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super. onCreate(savedInstanceState);
setContentView(R . layout . activity_main);
if(savedInstanceState == null ) {
getSupportFragmentManager() . beginTransaction()
.replace(R . id . container, MainFragment . newInstance())
.commit();