0

我讓我的代碼中的NPE。我試圖獲取所選RadioButton的值。當我點擊例外被炒魷魚的按鈕,不知道爲什麼,我可以想像,它需要的錯誤觀點......DialogFragment RadioGroup中的onClick NullPointerException異常

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> 

非常感謝你,高度讚賞你的幫助! :)

+0

哪裏u得到的NPO? – 2014-10-07 18:11:08

+0

@AlonLevanon我得到的NPO在內部類的onClick法。 – Khaled 2014-10-08 07:25:22

回答

2

你命名你的片段查看視圖,但你也有OnClick方法指的是按鈕本身,所以當你發現通過ID的單選按鈕的內部變量視圖,該計劃正在調查錯誤視圖。正在看按鈕而不是看你的片段。重命名OnClick方法變量視圖來避免這種混亂

而且你必須聲明視圖對象爲final,因此在匿名內部類的可用!

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(); 
     } 
    } 
+0

謝謝你的人,我在公共無效的onClick之前試過,但沒有奏效,仍是同樣的錯誤NPE。我編輯了代碼,我可以請你再看一遍嗎? :) – Khaled 2014-10-08 07:26:39

+0

現在我明白了。非常感謝你。 @Carlos J是完全正確的。我在代碼中添加了一些行,這樣每個人都可以看到正確的解決方案! ;) – Khaled 2014-10-08 08:51:04