实际上,我想在RadioGroup的setOnCheckedChangeListener中问你一个关于NPE的问题。我已经在15到23的API中测试了这个代码,只有崩溃的是API 15& 16。
起初,我不需要解决NPE,我做到了。我需要知道为什么android会给radioButton ID赋一个负值,这会导致'group.findViewById'中的NULL返回。
这里我们有代码:
private List<Question> listItems;
private Question getItem(int position) {
return listItems.get(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.questionLayout, parent, false);
return new VHQuestion(v);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final Question currentItem = getItem(position);
VHQuestion VHQuestion = (VHQuestion)holder;
RadioGroup group = new RadioGroup(context);
VHQuestion.listLayout.removeAllViewsInLayout();
if(currentItem.getOptions() != null){
for(int i = 0; i< currentItem.getOptions().size(); i++) {
CustomRadioButton radioButton = new CustomRadioButton(context);
radioButton.setText("Question " + i);
group.addView(radioButton, i);
if (currentItem.getAnswer() != null ) {
group.check(radioButton.getId());
// More code...
}
}
}
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID);
if (checkedButton.hasAnswer()) {
// More code...
}
}
});
VHQuestion.listLayout.addView(group);
}
class VHQuestion extends RecyclerView.ViewHolder{
LinearLayout listLayout;
public VHQuestion(View itemView) {
this.listLayout = (LinearLayout)itemView.findViewById(R.id.card_layout);
}
}
这里我们有xml“questionLayout.xml”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tool="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_marginLeft="@dimen/side_margin_left"
android:layout_marginRight="@dimen/side_margin"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:layout_width="match_parent"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/card_content_padding"
android:layout_marginLeft="10dp">
<LinearLayout
android:id="@+id/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
使用此代码,我遇到了这个问题:
group.check(radioButton.getId())
什么也没做。checkedButton
mID attr的负值(例如:-1352790336)。CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID)
返回null。findViewById
会返回 null 。if (checkedButton.hasAnswer())
抛出NPE。所以,我不知道为什么这个NPE被抛入这个API。
我已经解决了问题:
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radiogroup, int checkedId) {
if (shouldCallCheckedChange) {
shouldCallCheckedChange = false;
CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(checkedId);
if (android.os.Build.VERSION.SDK_INT < 17) {
for (int i = 0; i < group.getChildCount(); i++) {
((CustomRadioButton) group.getChildAt(i)).setChecked(false);
}
for (int i = 0; i < group.getChildCount(); i++) {
if (group.getChildAt(i).getId() == checkedId) {
checkedButton = (CustomRadioButton) group.getChildAt(i);
checkedButton.setChecked(true);
}
}
}
if (checkedButton.hasAnswer()) {
// More code...
}
shouldCallCheckedChange = true;
}
}
});
...
这段代码:
group.check(radioButton.getId());
替换为:
radioButton.setChecked(true);
所以我的问题是如何避免NPE: 为什么android会为在Dinamically创建的RadioButton分配负值?
谢谢大家!
答案 0 :(得分:0)
我正在测试我的应用程序...而且我已经看到
setOnCheckedChangeListener
具有过多功能
onCheckedChanged
在api15和api 16中返回单选按钮的负ID ... 在发帖时,我发现RadioButtons在xml中没有ID。 这似乎是个奇怪的错误...虽然它可以与大多数常见的android API一起使用,但不适用于api 15-16 ..我在xml上输入id后,它就可以在15-16上使用。
android:id="@+id/sunday"
只需为每个单选按钮提供ID,您就会看到它起作用