NullPointerException in RadioGroup.setOnCheckedChangeListener in APIS 15 & 16

徘徊边缘 提交于 2019-12-23 03:39:10

问题


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.

At first, i dont need to solve the NPE, i did it. I need to know why android assigns a negative value to radioButton ID, which causes NULL return in 'group.findViewById'.

Here we've the code:

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);
    }

}

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()) does nothing.
  • checkedButton mID attr has a negative value (e.g.: -1352790336).
  • CustomRadioButton checkedButton = (CustomRadioButton) group.findViewById(radioButtonID) returns null.
  • findViewByIdreturns null if ID value is negative.
  • if (checkedButton.hasAnswer()) throws 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;

        }

    }
});
...

And this code: group.check(radioButton.getId());

is replaced by: radioButton.setChecked(true);

So my question instead how to avoid the NPE is: Why android assigns negative value to RadioButton created dinamically?

THANK YOU ALL!!

来源:https://stackoverflow.com/questions/40631703/nullpointerexception-in-radiogroup-setoncheckedchangelistener-in-apis-15-16

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!