【发布时间】:2019-12-11 15:54:51
【问题描述】:
问题是:所以我有一个片段,其中有一个回收器视图,带有自定义适配器,它存储了一堆显示大学 ID 和名称的大学卡。他们的右侧还有一个按钮。这是这张卡片模板的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gradientEnd"
android:layout_margin="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/college_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="EXAMPLE COLLEGE"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:textColor="@color/colorPrimaryDark"
android:fontFamily="sans-serif-medium"/>
<TextView
android:id="@+id/college_id"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ID: 1234567"
android:textSize="16sp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:textColor="@color/colorPrimaryDark"
android:fontFamily="sans-serif-medium"/>
</LinearLayout>
<Button
android:id="@+id/btn_college_details"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Details"
android:textStyle="bold"
android:textColor="@color/colorPrimaryDark"
android:fontFamily="sans-serif-medium"
android:background="@drawable/btn_bg"
android:layout_marginBottom="10dp"
android:layout_marginEnd="5dp"
android:padding="5dp"
android:layout_marginTop="8dp"/>
</LinearLayout>
</LinearLayout>
因此,因为这是在片段内部的回收器视图中,所以当我尝试在onCreateView() 中为该按钮创建setOnClickListener 时,当我打开回收器视图所在的片段时,它就会崩溃。我认为这是因为回收器视图填充在片段中,同时我试图弄乱按钮,它根本看不到它,因为我得到一个java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 异常。下面是 Fragments Java 代码:
public class CollegeFragment extends Fragment implements ItemClickListener{
private RecyclerView recyclerView;
private CustomRecyclerAdapter customRecyclerAdapter;
private List<CollegeItem> collegeItemList = new ArrayList<>();
private DatabaseCollege dbCollege;
private EditText cName, cId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myFragmentView = inflater.inflate(R.layout.fragment_college, container, false);
dbCollege = new DatabaseCollege(getActivity());
recyclerView = myFragmentView.findViewById(R.id.recycler);
customRecyclerAdapter = new CustomRecyclerAdapter(dbCollege.getCollegeData(), getActivity()); //populate recycler view with college cards
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(customRecyclerAdapter);
customRecyclerAdapter.setItemClickListener(this);
cId = myFragmentView.findViewById(R.id.et_ipeds);
cId.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
CollegeFragment.this.customRecyclerAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
Button details = myFragmentView.findViewById(R.id.btn_college_details);
details.setOnClickListener(new View.OnClickListener() { //This line causes the crash
@Override
public void onClick(View v) {
//do stuff
}
});
return myFragmentView;
}
@Override
public void onClick(View v, int position) {
String name = collegeItemList.get(position).getName();
String id = collegeItemList.get(position).getId();
}
}
所以一般的问题是:如何编写能够定位按钮 setOnClickListener 的代码,该按钮位于回收器视图的生成项中,而回收器视图本身位于片段内部?
【问题讨论】:
标签: java android button nullpointerexception