2013-04-11 198 views
0

我在RadioGroup中有一個4 RadioButton的對話框,我試圖返回點擊的按鈕的ID,但我不斷收到NullPointerException。我看了幾個例子,並沒有看到我的不同。爲什麼RadioButton返回NULL?

<RadioGroup 
      android:id="@+id/radiojqmobdiv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checkedButton="0" 
      android:orientation="horizontal" > 

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

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

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

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

     </RadioGroup> 

代碼:

@Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     preferences = getSharedPreferences("MYPREFS", 0); 

     et = (EditTextLineNumbers) findViewById(R.id.ide); 
     et.setTextColor(preferences.getInt("colourChoice", Color.GREEN)); 
     et.setMaxLines(5000); 
     divBtn = (Button) findViewById(R.id.divbutton); 
     divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv); 

     exists = false; 
     gestureDetector = new GestureDetector(this, new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }; 
     web = (WebView) findViewById(R.id.webpreview); 
     web.setOnClickListener(this); 
     web.setOnTouchListener(gestureListener); 
     getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
     File dir = new File(Environment.getExternalStorageDirectory() 
       + "/My Webs"); 
     currentDirectory = dir; 
     ListView lv = getListView(); 
     registerForContextMenu(lv); 
     lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

      @Override 
      public boolean onItemLongClick(AdapterView<?> a, View v, 
        int position, long id) { 
       Show_Alert_box(v.getContext(), "Please select action.", 
         position); 
       return false; 
      } 
     }); 

     et.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       changed = true; 
       startPos = et.getSelectionStart(); 
       endPos = et.getSelectionEnd(); 
      } 
     }); 
     if (dir.isDirectory()) { 
      browseToRoot(); 
     } else { 
      dir.mkdir(); 
     } 

     divBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       final Dialog dialog = new Dialog(MainActivity.this); 
       dialog.setContentView(R.layout.divdialog); 
       dialog.setTitle("Inserv Div"); 

       // set the custom dialog components - text, image and button 
       // TextView text = (TextView) dialog.findViewById(R.id.text); 
       // text.setText("Android custom dialog example!"); 
       // ImageView image = (ImageView) 
       // dialog.findViewById(R.id.image); 
       // image.setImageResource(R.drawable.ic_launcher); 

       **Button insertButton = (Button) dialog 
         .findViewById(R.id.insertBtn);** 
       // if button is clicked, close the custom dialog 
       insertButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // int selectedID = divGroup.getCheckedRadioButtonId(); 

         // find the radiobutton by returned id 
         divRdoBtn = (RadioButton) findViewById(selectedID); 

         Toast.makeText(MainActivity.this, divRdoBtn.getText(), 
           Toast.LENGTH_LONG).show(); 
        } 
       }); 

       Button cancelButton = (Button) dialog 
         .findViewById(R.id.cancelBtn); 
       cancelButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 
       dialog.show(); 
      } 

     }); 

     gestureDetector = new GestureDetector(this, new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }; 
     loadPrefs(); 
    } 
+0

WHEREre stacktrace? – Smit 2013-04-11 00:18:32

+0

divGroup被初始化爲onCreate(); NullException發生在int selectedID ... – RapsFan1981 2013-04-11 00:20:43

+1

然後'divGroup'爲'null' – codeMagic 2013-04-11 00:21:31

回答

1

你不發表您的對話框佈局和活動佈局兩個完整的XML文件,所以我在這裏做一些假設。

但是,您在標題中說過Dialog包含您已命名爲divGroup的RadioGroup。如果這是真的,這是有道理的,當你執行這條線,你將無法找到RadioGroup中:

divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv); 

您在活動的ID R.id.radiojqmobdiv搜索,你已經暗示僅存在於尚未創建的對話框中。如果沒有找到視圖,findViewById()將返回null,因此您將空值分配給divGroup

在聲明對話框後,應該將該行向下移動,並確保在dialog對象上而不是在Activity上調用findViewById

final Dialog dialog = new Dialog(MainActivity.this); 
dialog.setContentView(R.layout.divdialog); 
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv); 
+0

Ahhhtesting現在,但我有一種感覺,這將工作是有道理的 – RapsFan1981 2013-04-11 01:06:07

+0

好! :)讓我知道它是否有效,或者如果我可以添加到幫助更多的答案。 – mattgmg1990 2013-04-11 01:13:56