Java 为操作1选择的OnOptions Item未显示新警报对话框

Java 为操作1选择的OnOptions Item未显示新警报对话框,java,android,sdk,Java,Android,Sdk,在我的onOptions ItemSelected方法中,当用户点击操作设置1时,我的新警报对话框不显示 package com.karanvir.search; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; impo

在我的
onOptions ItemSelected
方法中,当用户点击操作设置1时,我的新警报对话框不显示

package com.karanvir.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.karanvir.search.MainActivity.urlGlobal;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //finding webview
        WebView webView=(WebView) findViewById(R.id.web1);

//whole bunch of settings you might want to do if you .
        //do this because javascript is so wildely used that if you dont use this anywebsites you display wont be displayed properly
        webView.getSettings().setJavaScriptEnabled(true);
        //this is because on a number of phones if you dont do this it will jump to the devices default browser, and ddisplay the websview their instead.
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://www.google.ca/?gws_rd=ssl#q="+urlGlobal);
        //reminder ask permission
        //you can load content using loaddata
        //then add type of data
        //then add character encoded were using

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings4) {
            Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intentGoogle);



            return true;
        } else if(id ==R.id.action_settings2){
            Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
            startActivity(intentGoogle);


            return true;

        }else if (id==R.id.action_settings3){
            Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intentGoogle);


            return true;

        }else if(id==R.id.action_settings1){
            new AlertDialog.Builder(getApplicationContext())
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle("About")
                    .setMessage("This app interchanges between different search engines, to help guess what your looking for. We dont not acess your location, also we do not save any information. Right now we are working on a way to make the app completely private in congnito.Email us if you want to help with this project also. Please email Dhillonapps93@gmail.com for help/inquries. We run this app with no ads, we also accept donations at runescapegold1291@hotmail.com. Yes I used to play runescape lol. Thanks for reading and enjoy");
            return true;

        }

        return super.onOptionsItemSelected(item);
    }

 }
您忘记在新对话框上调用.show()。您只创建了它;)


您只需编写alertDialog.show();要显示对话框。。。。。。示例如下所示

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            getActivity());
    // Setting Dialog Title
    alertDialog.setTitle("Title is here");

    // Setting Dialog Message
    alertDialog.setMessage("Message is here");

    // On pressing ok button
    alertDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            //on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                    //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            // Showing Alert Message
    alertDialog.show();
    alertDialog.setCancelable(false);

我在回答中编辑了一件事,那就是将活动用作上下文,而不是应用程序上下文。其次,你在活动或应用程序上使用AppCompat主题吗?还是不起作用,顺便说一句,我有多个活动。然而,这是主要的,它不起作用。我只是想把它扔出去
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            getActivity());
    // Setting Dialog Title
    alertDialog.setTitle("Title is here");

    // Setting Dialog Message
    alertDialog.setMessage("Message is here");

    // On pressing ok button
    alertDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            //on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                    //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            // Showing Alert Message
    alertDialog.show();
    alertDialog.setCancelable(false);