Android 在OnOptions ItemSelected中使用哪个上下文?

Android 在OnOptions ItemSelected中使用哪个上下文?,android,this,alert,android-context,Android,This,Alert,Android Context,我想在onOptionsItemSelected中使用AlertDialog,但我不知道要使用哪个上下文。 getApplication()和getApplicationContext()都不起作用。有什么想法吗 public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically hand

我想在
onOptionsItemSelected
中使用
AlertDialog
,但我不知道要使用哪个上下文。
getApplication()
getApplicationContext()
都不起作用。有什么想法吗

public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.actionbar_menu_info) {

        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.container, new FragmentInfo())
            .commit();

    }
    else if (id == R.id.actionbar_menu_settings) {

      //            getSupportFragmentManager()
      //                .beginTransaction()
      //                .replace(R.id.container, new FragmentSettings())
      //                .commit();

        AlertDialog.Builder builder = new AlertDialog.Builder(get);
        builder.setIcon(getResources().getDrawable(R.drawable.quiz_ic_dialog_wrong));
        builder.setTitle(getResources().getString(R.string.ad_no_settings_in_free_version_title));
        builder.setMessage(getResources().getString(R.string.ad_no_settings_in_free_version_message));
        builder.setNeutralButton("Ok", null);
        builder.setCancelable(true);
        AlertDialog alert = builder.create();
        alert.show();

    }
    return super.onOptionsItemSelected(item);
}

以下是您的答案:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {

        case R.id.yourother_id:

            //Your Code!

            break;

        case R.id.your_id_thats_supposed_to_show_dialog:

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
            builder.setTitle(getResources().getString(R.string.hello_world));
            builder.setMessage(getResources().getString(R.string.hello_world));
            builder.setNeutralButton("Ok", null);
            builder.setCancelable(true);
            AlertDialog alert = builder.create();
            alert.show();

            break;

        default:
            break;
        }


        return super.onOptionsItemSelected(item);
    }

希望这能奏效:)

这是你的答案:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {

        case R.id.yourother_id:

            //Your Code!

            break;

        case R.id.your_id_thats_supposed_to_show_dialog:

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
            builder.setTitle(getResources().getString(R.string.hello_world));
            builder.setMessage(getResources().getString(R.string.hello_world));
            builder.setNeutralButton("Ok", null);
            builder.setCancelable(true);
            AlertDialog alert = builder.create();
            alert.show();

            break;

        default:
            break;
        }


        return super.onOptionsItemSelected(item);
    }

希望这能起作用。:)

尝试使用您的活动。这作为上下文。有效!谢谢,但我需要写
这个
,而不是
活动。这个
尝试使用你的活动。这个作为上下文。工作!谢谢,但我需要写
这个
,而不是
活动。这个
谢谢!这对我来说是可行的,但是设计Dialog的最佳实践建议使用:@FrancisRodrigues这段代码只是警报对话框的一个例子,它非常古老。是的,有更新更好的方法来创建对话。谢谢!这对我来说是可行的,但是设计Dialog的最佳实践建议使用:@FrancisRodrigues这段代码只是警报对话框的一个例子,它非常古老。是的,有更新更好的方法来创建对话框。