2017-10-02 5 views
0

Mon AlertDialog ne montre pas setpositive bouton oui et setnegative bouton pas dans le périphérique réel, mais montrant dans l'émulateur. Lorsque je cours l'application sur un appareil réel, il affiche la boîte de dialogue d'alerte, mais pas oui et aucun dans le périphérique réel. L'application fonctionne très bien sur l'émulateur. Cela fonctionne très bien sur le vrai périphérique mais les boutons ne sont pas visibles.AlertDialog ne fonctionne pas correctement sur l'appareil réel, mais sur l'émulateur

private void showAddFoodDialog() { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(FoodList.this); 
    alertDialog.setTitle("Add new Food"); 
    alertDialog.setMessage("please fill full information"); 

    LayoutInflater inflater=this.getLayoutInflater(); 
    View add_menu_layout= inflater.inflate(R.layout.add_new_food_layout,null); 

    edtName = (MaterialEditText) add_menu_layout.findViewById(R.id.edtName); 
    edtDescription = (MaterialEditText) add_menu_layout.findViewById(R.id.edtDescription); 
    edtPrice = (MaterialEditText) add_menu_layout.findViewById(R.id.edtPrice); 
    edtDiscount = (MaterialEditText) add_menu_layout.findViewById(R.id.edtDiscount); 
    btnSelect = (FButton) add_menu_layout.findViewById(R.id.btnSelect); 
    btnUpload = (FButton) add_menu_layout.findViewById(R.id.btnUpload); 

    //event for button 
    btnSelect.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      chooseImage(); //let user select image from gallery and save uri of this image 
     } 
    }); 

    btnUpload.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      uploadImage(); 
     } 
    }); 
    alertDialog.setView(add_menu_layout); 
    alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp); 

    //set button 
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
      //here ,just create new category 
      if (newFood !=null) 
      { 
       foodList.push().setValue(newFood); 
       Snackbar.make(rootLayout,"New category "+newFood.getName()+" was added",Snackbar.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 
    alertDialog.show(); 
} 
+0

La compétence la plus importante que vous aurez jamais apprendre est comment déboguer. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jdv

Répondre

0

Créer une alerte Close avant de montrer.

AlertDialog dialog = alertDialog.create(); 
dialog.show(); 
0

C'est l'exemple pour créer une bonne boîte de dialogue dans Android

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setMessage("Are you sure, 
    You wanted to make decision"); 
    alertDialogBuilder.setPositiveButton("yes", 
    new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface arg0, int arg1) { 
     Toast.makeText(MainActivity.this,"You clicked yes 
      button",Toast.LENGTH_LONG).show(); 
    } 
    }); 

    alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() { 
    Override 
    public void onClick(DialogInterface dialog, int which) { 
     finish(); 
    } 
    }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
}