これを試してください、うまくいくはずです:
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.Login:
Toast.makeText(getApplicationContext(), "Fired 1", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, Login.class));
return true;
case R.id.About:
Toast.makeText(getApplicationContext(), "Fired 2", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, About.class));
return true;
case R.id.Post_Ads:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
startActivity(new Intent(this, Login.class));
return true;
}
return super.onOptionsItemSelected(item);
}
topublic boolean onOptionsItemSelected(MenuItem item, int id)
で呼び出されるメソッドはありませんActivity
class
handle option menu's.
このようなイベントを処理するメソッドは onOptionsItemSelected(MenuItem)
.
メニュー項目を表示するには、次のメソッドも使用する必要があります。このメソッドも実装しているかどうかを確認してください。
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_file, menu);
return true;
}
そしてちょっとした訂正、
次の Alertdialog メソッドを実装しました。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
startActivity(new Intent(this, Login.class));
ここでは [OK] または [キャンセル] ボタンを提供していません。また、setCancellable(false) にしました。そのため、ユーザーはここでオプションを選択できません。
したがって、ここで [OK] ボタンと [キャンセル] ボタンを指定して、onClick() メソッド内でやりたいことを実行することをお勧めします。次の例のように:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false);
AlertDialog alert = builder.create();
alert.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do call some activity or close the app. Do what you wish to;
}
});
alert.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do stay in the current activity or something else. Do what you wish to;
}
});
alert.show();
return true;
希望、それは役立つかもしれません。