RecyclerView 内部的视图返回 null
View inside of RecyclerView is returning null
我正在尝试为我的 CheckBox 设置一个 onClickListener,但我的 checkBox 实例在 onClickListener 上返回 null,给我一个 nullPointerException。我已经查看了与我的类似的其他问题,但没有找到任何有效的方法。我对 RecyclerView 不太熟悉,所以如果它与我初始化视图的方式有关,我不会感到惊讶。
这是有问题的代码-
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 并尝试将复选框映射到该布局。
您可以将复选框映射到视图持有者中,并在适配器的 onBindViewHolder 中设置 onclick 侦听器 class
您应该 findViewById
在您的 MilestonesAdapter
中,MilestonesAdapter
负责为 RecyclerView
创建项目。
从 Android 开发者网站查看 guide。
检查有关 onBindViewHolder
的内容。
我正在尝试为我的 CheckBox 设置一个 onClickListener,但我的 checkBox 实例在 onClickListener 上返回 null,给我一个 nullPointerException。我已经查看了与我的类似的其他问题,但没有找到任何有效的方法。我对 RecyclerView 不太熟悉,所以如果它与我初始化视图的方式有关,我不会感到惊讶。
这是有问题的代码-
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 并尝试将复选框映射到该布局。
您可以将复选框映射到视图持有者中,并在适配器的 onBindViewHolder 中设置 onclick 侦听器 class
您应该 findViewById
在您的 MilestonesAdapter
中,MilestonesAdapter
负责为 RecyclerView
创建项目。
从 Android 开发者网站查看 guide。
检查有关 onBindViewHolder
的内容。