Tentei implementar um AlertDialog no meu código e recebi o seguinte erro...
public void onCreateContextMenu(final ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(info.position);
MenuItem itemLigar = menu.add("Ligar");
itemLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if(ActivityCompat.checkSelfPermission(ListaAlunosActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ListaAlunosActivity.this,
new String[]{Manifest.permission.CALL_PHONE}, 1);
} else {
Intent intentLigar = new Intent(Intent.ACTION_CALL);
intentLigar.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentLigar.setData(Uri.parse("tel:" + aluno.getTelefone()));
startActivity(intentLigar);
}
return false;
}
});
MenuItem itemSMS = menu.add("Enviar SMS");
Intent intentSMS =new Intent(Intent.ACTION_VIEW);
intentSMS.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentSMS.setData(Uri.parse("sms:" + aluno.getTelefone()));
itemSMS.setIntent(intentSMS);
MenuItem itemMapa = menu.add("Visualizar no mapa");
Intent intentMapa = new Intent(Intent.ACTION_VIEW);
intentMapa.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentMapa.setData(Uri.parse("geo:0,0?q=" + aluno.getEndereco()));
itemMapa.setIntent(intentMapa);
MenuItem itemSite = menu.add("Visitar site");
Intent intentSite = new Intent(Intent.ACTION_VIEW);
intentSite.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String site = aluno.getSite();
if(!site.startsWith("http://")) {
site = "http://" + site;
}
intentSite.setData(Uri.parse(site));
itemSite.setIntent(intentSite);
MenuItem deletar = menu.add("Excluir");
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
new AlertDialog
.Builder(getApplicationContext())
.setTitle("Removendo aluno")
.setMessage("Tem certeza que quer remover o aluno?")
.setPositiveButton("Sim", (DialogInterface.OnClickListener) item)
.setNegativeButton("Não", (DialogInterface.OnClickListener) item)
.show();
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deleta(aluno);
dao.close();
Toast.makeText(ListaAlunosActivity.this, "Aluno " + aluno.getNome() +" excluido!",Toast.LENGTH_SHORT).show();
carregaLista();
return false;
}
});
}
E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.juaanhs.agenda, PID: 19777 java.lang.ClassCastException: com.android.internal.view.menu.MenuItemImpl cannot be cast to android.content.DialogInterface$OnClickListener at br.com.juaanhs.agenda.ListaAlunosActivity$4.onMenuItemClick(ListaAlunosActivity.java:156) at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:144) at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:923) at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:913) at com.android.internal.view.menu.MenuPopup.onItemClick(MenuPopup.java:128) at android.widget.AdapterView.performItemClick(AdapterView.java:318) at android.widget.AbsListView.performItemClick(AbsListView.java:1159) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136) at android.widget.AbsListView$3.run(AbsListView.java:4052) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)