APIS 15和16中RadioGroup.setOnCheckedChangeListener中的NullPointerException

问题描述 投票:1回答:1

实际上,我要问您有关RadioGroup的setOnCheckedChangeListener中的NPE的问题。我已经在15到23的API中测试了此代码,只有崩溃的是API 15和16。

起初,我不需要解决NPE,我做到了。我需要知道为什么android为单选按钮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属性具有负值(例如:-13522790336)。
  • [CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID)返回null。
  • 如果ID值为
  • 负,findViewById返回null
  • if (checkedButton.hasAnswer())抛出NPE。

所以,我不知道为什么在此API中抛出此NPE。

我已经解决了已经解决的问题:

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分配负值?

谢谢大家!

android nullpointerexception radio-button radio-group
1个回答
0
投票

我正在测试我的应用程序...我已经看到了

setOnCheckedChangeListener

具有叠加功能

onCheckedChanged

在api15和api 16中返回单选按钮的负ID ...在发帖时,我知道RadioButtons在xml中没有id ...这似乎是个奇怪的错误...虽然它可以与大多数常见的android API一起使用,但不适用于api 15-16。

android:id="@+id/sunday"

只需为每个单选按钮提供ID,您就会看到它的工作原理

© www.soinside.com 2019 - 2024. All rights reserved.