2017-07-07 1 views
1

Sur ma méthode onOptionsItemSelected lorsque l'utilisateur tape sur les paramètres d'action 1, ma nouvelle boîte de dialogue d'alerte ne s'affiche pas?OnOptionsItemSélectionné pour l'action 1 ne pas afficher une nouvelle boîte de dialogue d'alerte

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 [email protected] for help/inquries. We run this app with no ads, we also accept donations at [email protected] Yes I used to play runescape lol. Thanks for reading and enjoy"); 
      return true; 

     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

Répondre

2

Vous avez oublié d'appeler .show() dans la nouvelle boîte de dialogue. Vous l'avez seulement créé;)

else if(id==R.id.action_settings1){ 
    new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_info) 
      .setTitle("About") 
      .setMessage(".....") 
      .show(); 
    return true; 
} 
+0

J'ai essayé un, il ne fonctionne toujours pas –

+0

j'édité une chose dans ma réponse, et qui est d'utiliser l'activité comme le contexte et non la contexte de l'application. Deuxièmement, utilisez-vous un thème AppCompat sur l'activité ou l'application? – stephanmantel

+0

ne fonctionne toujours pas, j'ai plusieurs activités. Cependant, c'est pour le principal, et ça ne marche pas. juste pensé id jeter là-bas –

0

vous devez juste écrire alertDialog.show(); pour montrer l'dialoge ...... échantillon est donnée ci-dessous

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);