APIS 15 中 RadioGroup.setOnCheckedChangeListener 中的 NullPointerException &16 [英] NullPointerException in RadioGroup.setOnCheckedChangeListener in APIS 15 & 16
问题描述
其实我想问你一个关于 RadioGroup 的 setOnCheckedChangeListener 中 NPE 的问题.我已经在 15 到 23 的 API 中测试了这段代码,只有 API 15 & 崩溃了.16.
Actually, I've to ask you a question about NPE in setOnCheckedChangeListener of RadioGroup. I've tested this code in APIs from 15 to 23, and only crashes is APIs 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"
Here we've the 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>
使用此代码,我遇到了以下问题:
With this code, i've this problems:
group.check(radioButton.getId())
什么都不做.checkedButton
mID attr 具有负值(例如:-1352790336).CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID)
返回 null.findViewById
如果 ID 值为负,则返回null.if (checkedButton.hasAnswer())
抛出 NPE.
group.check(radioButton.getId())
does nothing.checkedButton
mID attr has a negative value (e.g.: -1352790336).CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID)
returns null.findViewById
returns null if ID value is negative.if (checkedButton.hasAnswer())
throws NPE.
所以,我不知道为什么在这个 API 中抛出这个 NPE.
So, i dont know why this NPE is thrown in this APIs.
我已经解决了这个问题:
I've solved the problem already doing it:
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为动态创建的RadioButton分配负值?
So my question instead how to avoid the NPE is: Why android assigns negative value to RadioButton created dinamically?
谢谢大家!!
推荐答案
我正在测试我的应用程序...我已经看到
I was testing my application... And I have seen
setOnCheckedChangeListener
setOnCheckedChangeListener
具有覆盖功能
onCheckedChanged
onCheckedChanged
为 api15 和 api 16 中的 radioButtons 返回负 ID...在测试时,我认识到 RadioButtons 在 xml 中没有 id ...这似乎是个奇怪的错误......虽然它适用于最常见的 android api,但它不适用于 api 15-16 .. 在我在 xml 上给出 id 后,它适用于 15-16 .. 这是示例
was return negative id for radioButtons in api15 and api 16... While tetsing, I recognize RadioButtons had not id in xml... This seems weird bug... While it is working with most common android apis it does not work with api 15-16.. after I give id on xml, it is worked on 15-16.. Here is example
android:id="@+id/sunday"
只需为每个单选按钮提供 id,您就会看到它的工作
just give id to each radiobuttons, you will see it work
这篇关于APIS 15 中 RadioGroup.setOnCheckedChangeListener 中的 NullPointerException &16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!