2017-01-17 4 views
2

オーバーフローメニュー項目からアラートダイアログを起動しようとしています。ダイアログレイアウトはdialog_settings.xmlにあります。Android:オーバーフローメニュー項目からアラートダイアログを起動する

import android.support.v7.app.AlertDialog; 

/** Code omitted */ 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(): 
     if (id == R.id.action_settings) { 
      AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this); 
      View mView = getLayoutInflater().inflate(R.layout.dialog_settings, null); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

上記のコードではエラーは発生しませんが、メニュー項目をクリックするとダイアログが表示されません。私は自分のアプリとAndroid StudioにAndroid 4.1.2、API 16を使用しています。

このquestion from 2012よりも簡単な解決策を見つけることを望みます。

答えて

1

ダイアログを作成しているだけで、作成して表示しているわけではありません。 多分このようなものが欲しいでしょう:

if(id == R.id.action_settings) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(R.layout.dialog_settings) //set the view 
     .create() //create the dialog 
     .show(); //show the dialog 

    return true; 
} 
関連する問題