2011-08-11 21 views
2

AlertDialogを作成する際に問題が発生しているようです。私がしようとしているのは、特定のRadioButtonがクリックされたときにポップアップするカスタムAlertDialogを作成することです。ここに私の関連するコードは次のとおりです。AlertDialogの作成に関する問題

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     m_Minutes = -1; 
     m_this=this; 

     String prefName = getString(R.string.prefsfile); 
     SharedPreferences settings = getSharedPreferences(prefName, 0); 
     String defaultTextBackMessage = settings.getString("textBackMessage", getString(R.string.defaultTextBackMessage)); 
     EditText txtMessage = (EditText)findViewById(R.id.editText1); 
     txtMessage.setText(defaultTextBackMessage); 

     final Button button = (Button)findViewById(R.id.button1); 
     final RadioButton manualButton = (RadioButton)findViewById(R.id.radio0); 
     final RadioButton button15 = (RadioButton)findViewById(R.id.radio1); 
     final RadioButton button30 = (RadioButton)findViewById(R.id.radio2); 
     final RadioButton customButton = (RadioButton)findViewById(R.id.radio3); 

     manualButton.setChecked(true); 
     customButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Context c = v.getContext(); 
       LayoutInflater factory = LayoutInflater.from(v.getContext()); 
       final View minuteEntryView = factory.inflate(R.layout.customtime, null); 
       AlertDialog ad = AlertDialog.Builder(c).create(); // this is the trouble line 
      } 
     }); 

私はAlertDialog ad = AlertDialog.Builder(c).create();行でエラーを取得しています。私が得ているエラーはThe Method Builder(Context) is undefined for the type AlertDialogです。しかし、明らかに、Google API DocsにはBuilderコンストラクタがあります。私は間違って何をしていますか?

+0

エラーログを表示できますか? –

+0

@Suri - ランタイムエラーではなく、コンパイルエラーが発生しています – Icemanind

+0

私は同じ問題に直面しました。私は "新しい"を忘れてしまったとは思いません。私は休憩の男が必要です! – Ishaan

答えて

8

あなたがこれを言うshould't ...

AlertDialog ad = new AlertDialog.Builder(c).create(); 

あなたは忘れてしまったnewキーワード。 明らかにわかるように、その方法は見つかりませんでした。 hは、通常の方法でコンストラクタを呼び出すが、メソッドを呼び出す方法であることを意味します。

+0

これは私の問題でした。おかげでntc! – Icemanind

0

アクティビティでonCreateDialog(int id)メソッドをオーバーライドする必要があります。そのメソッドの中でDialogオブジェクトを作成し、RadioButtonのonClickイベントからshowDialog(id)メソッドを使用してダイアログを呼び出す必要があります。

コード以下を参照してください。

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    // TODO Auto-generated method stub 

    AlertDialog dialog = null; 
    AlertDialog.Builder builder = null; 

    builder = new AlertDialog.Builder(this); 

    switch(id) 
    { 
    case USERNAME_PASSWORD_EMPTY: 

     builder.setMessage("Please Enter Username and Password."); 
     builder.setCancelable(false); 

     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int id) 
      { 
       //Do what you want to do when user clicks OK button of dialog 
      } 
     }); 

     dialog = builder.create(); 

    break; 
    } 

    return dialog; 
} 

あなたは以下のようにShowDialog(ID)メソッドを使用して、このダイアログを呼び出す必要があります。

showDialog(USERNAME_PASSWORD_EMPTY); 
関連する問題