gpt4 book ai didi

android - RecyclerView 内部的 View 返回 null

转载 作者:行者123 更新时间:2023-11-30 00:25:39 25 4
gpt4 key购买 nike

我正在尝试为我的 CheckBox 设置一个 onClickListener,但我的 checkBox 实例在 onClickListener 上返回 null,给我一个 nullPointerException。我已经查看了与我的类似的其他问题,但没有找到任何有效的方法。我对 RecyclerView 不太熟悉,所以如果它与我初始化 View 的方式有关,我不会感到惊讶。

这是有问题的代码-

Months0Through6.java

public class Months0Through6 extends android.support.v4.app.Fragment {

public Months0Through6() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

CheckBox checkBox;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

//This is returning null
checkBox = (CheckBox)rootView.findViewById(R.id.checkBox_ID);

RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MilestonesAdapter adapter = new MilestonesAdapter(new String[]{"Month 0 stuff", "Example Two", "Example Three", "Example Four", "Example Five" , "Example Six" , "Example Seven"});
rv.setAdapter(adapter);

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);

checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),
"Your Message", Toast.LENGTH_LONG).show();

}
});

return rootView;
}

card_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="68dp">

<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="62dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv_text"
android:layout_toRightOf ="@+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TextView>

<TextView
android:id="@+id/tv_blah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Another Recyclerview Fragment"
android:layout_below="@+id/tv_text"
android:layout_toRightOf="@+id/checkBox_ID"
android:layout_toEndOf="@+id/checkBox_ID">
</TextView>

</RelativeLayout>
</android.support.v7.widget.CardView>

</RelativeLayout>

最佳答案

您的复选框位于 card_item 布局内。但是您正在膨胀 fragment_blank 并尝试将复选框映射到该布局。

您可以将复选框映射到 View 持有者中,并在适配器类的 onBindViewHolder 中设置 onclick 监听器

关于android - RecyclerView 内部的 View 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com