0

我的代码上出现了 NPE。我正在尝试获取所选 RadioButton 的值。当我单击按钮时,异常被触发,不知道为什么,我可以想象它采取了错误的观点......

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class CategoryDialog extends DialogFragment{

Button btnDone;
RadioGroup rg;
RadioButton radioBtn;
String selectedCategory;
Communicator communicator;

public static interface Communicator{
    public void onCategorySelected(String category);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    communicator = (Communicator) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
    this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
    btnDone = (Button) view.findViewById(R.id.category_done_btn);
    rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
    btnDone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int selectedId = rg.getCheckedRadioButtonId();
            radioBtn = (RadioButton) v.findViewById(selectedId);
            selectedCategory = radioBtn.getText().toString();
            if(v.getId() == R.id.category_done_btn){
                communicator.onCategorySelected(selectedCategory);
                dismiss();
            }
        }
    });
    return view;
  }

}

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RadioGroup
    android:id="@+id/category_radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" >

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/z2_videodetail_category_dialog_radioButton1" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton2" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton3" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton4" />   
</RadioGroup>

<Button
    android:id="@+id/category_done_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/category_radioGroup"
    android:text="@string/z2_videodetail_category_dialog_done_btn" />

</RelativeLayout>

非常感谢您,非常感谢您的帮助!:)

4

1 回答 1

2

您将片段视图命名为视图,但在 onClick 方法中也有变量视图引用按钮本身,因此当您通过 id 查找单选按钮时,程序正在查看错误的视图。正在查看按钮内部而不是查看您的片段。重命名 onClick 方法上的变量视图以避免这种混淆

而且您必须将视图对象声明为最终对象,以便它在匿名内部类中可用!

    final View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
    this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
    final RadioGroup rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);

    @Override
    public void onClick(View v) {    //Change it from view to v or whatever you want
        int selectedId = rg.getCheckedRadioButtonId();
        radioBtn = (RadioButton) view.findViewById(selectedId);
        selectedCategory = radioBtn.getText().toString();
        if(v.getId() == R.id.category_done_btn){ //And update this value
            communicator.onCategorySelected(selectedCategory);
            dismiss();
        }
    }
于 2014-10-07T18:23:29.427 回答