2012-02-24 16 views
0

我希望能夠關閉用戶選項並希望關閉對話框中的某些單選按鈕。我宣佈這樣的單選按鈕在OnCreate部分在對話框中使用單選按鈕

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    if (id > 0) 
    { 
     LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View layout = layoutInflater.inflate(R.layout.lectsort_dialog, (ViewGroup) findViewById(R.id.lect_sort)); 

       builder.setView(layout); 

     // Now configure the AlertDialog 
     builder.setTitle(R.string.exsort_title); 

     radio_date = (RadioButton) findViewById(R.id.RBdate); 
     radio_loctn = (RadioButton) findViewById(R.id.RBloctn); 
     radio_stream = (RadioButton) findViewById(R.id.RBstream); 
     radio_date.setOnClickListener(radio_listener); 
     radio_loctn.setOnClickListener(radio_listener); 
     radio_stream.setOnClickListener(radio_listener); 

     builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
      } 

的radio_listener過程聲明這樣

RadioButton.OnClickListener radio_listener = 
     new RadioButton.OnClickListener() 
     { 
     @Override 
     public void onClick(View v) { 
      // Perform action on clicks 
      RadioButton rb = (RadioButton) v; 
      Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();   
     } 
}; 

然而,當對話框被調用時,我得到一個空異常錯誤在此行

radio_date.setOnClickListener(radio_listener); 

我在做什麼錯?

回答

1

如果你想獲得一個UI元素的ID這是位於一個對話框中,你需要這樣做。

Dialog dialog = new Dialog(mContext); // your dialog creation method here 
    RadioButton radio_date = (RadioButton) dialog.findViewById(R.id.RBdate); 

如果使用findViewById那麼你想捕捉的是與當前胡亞蓉視圖(你將在setContentView API中已設置)

所以它試圖找到一個相關的視圖對象在活動視圖中使用ID爲RBdate的視圖無法查找,因此返回空值。

+0

感謝您的回覆,我敢肯定這是正確的。不過,我已經添加了以下代碼Context mContext = getApplicationContext();對話框=新對話框(mContext); RadioButton radio_date =(RadioButton)dialog.findViewById(R.id.RBdate);但是我仍然得到一個空異常錯誤,我是否在錯誤的地方宣佈它? – user616076 2012-02-24 11:21:47

+0

你確定你在相同的地方(對於RBdate)得到異常嗎?或用於不同的單選按鈕。在調試模式下運行你的應用程序並逐步找出確切的位置 – bluefalcon 2012-02-24 11:33:14

+0

是的,我在這裏得到錯誤 - radio_date.setOnClickListener(radio_listener);如果我嘗試不同的方式,我得到這裏的錯誤 - 如果(radio_date.isChecked()) – user616076 2012-02-24 11:41:09

0

檢查下面的代碼 這是單選按鈕上按一下按鈕

final AlertDialog.Builder builder = new AlertDialog.Builder(this); 

btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //int states = {false, false}; 
      builder.setTitle("Select the item that you want to delete from that item "); 
      builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int id) { 

        // TODO Auto-generated method stub 
        if(id==0){ 

        } 
        else 
        { 

      }); 

      alert= builder.create(); 
      alert.show(); 
     } 
    }); 

ü嘗試這一點,並讓報警我知道

相關問題