Android 我无法关闭“自定义警报”对话框
我需要创建一个alertdialog对话框,其中有两个按钮作为视图。我创建了它,但问题是在单击其中一个视图时执行操作后,我无法取消它。这意味着在第一个按钮中 这就是我要找的 我的叠加类Android 我无法关闭“自定义警报”对话框,android,android-alertdialog,android-custom-view,android-overlay,Android,Android Alertdialog,Android Custom View,Android Overlay,我需要创建一个alertdialog对话框,其中有两个按钮作为视图。我创建了它,但问题是在单击其中一个视图时执行操作后,我无法取消它。这意味着在第一个按钮中 这就是我要找的 我的叠加类 public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> mapOverlays = new ArrayList<Overla
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
DatabaseHandler db;
private Context context;
double lat;
double lon;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
db = new DatabaseHandler(context);
// latitude & longtitude (double)
lat = geopoint.getLatitudeE6() / 1E6;
lon = geopoint.getLongitudeE6() / 1E6;
}
return false;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
final String title= item.getTitle();
final String snippet= item.getSnippet();
if(title.equalsIgnoreCase("Your Location")){
alert.showpickAlertDialog1(context, title, snippet);
}
else if(title.equalsIgnoreCase("0")||title.equalsIgnoreCase("2")||title.equalsIgnoreCase("3")){
String lati = String.valueOf(lat);
String longi = String.valueOf(lon);
alert.showpickAlertDialog2(context, lati, longi, snippet);
}
else if(title.equalsIgnoreCase("1")||title.equalsIgnoreCase("4")||title.equalsIgnoreCase("5")||title.equalsIgnoreCase("6")){
}
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
public class AlertDialogManager {
private Context context;
DatabaseHandler db = new DatabaseHandler(context);
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
// Showing Alert Message
alertDialog.show();
}
public void showpickAlertDialog2(final Context context, final String lat, final String lon,final String snippet) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
final Button park_button = new Button(context);
park_button.setHint("Park here");
// park_button.setBackgroundResource();
layout.addView(park_button);
park_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("parkbutton:Clicked");
// Delete DB Values
int count = db.getDatasCount();
for(int i = 0;i<count;i++){
db.deleteValues(i);
}
//latitude & longtitude (string)
String latitude = lat;
String longtitude = lon;
Log.d("Insert: ", "Inserting ..");
db.addData(new Datas(latitude, longtitude));
// Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
// Reading DB Data
Log.d("Reading: ", "Reading all Values...");
List<Datas> datas = db.getAllDatas();
for (Datas dat : datas) {
String log = "Id: "+dat.getID()+" ,Latitude: " + dat.getlat() + " ,Longtitude: " + dat.getlon();
// Writing DB data to log
Log.d("DB Data: ", log);
}
park_button.setEnabled(false);
}
});
Button know_more_button = new Button(context);
know_more_button.setHint("Know more");
// park_button.setBackgroundResource();
layout.addView(know_more_button);
know_more_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_REFERENCE", snippet);
editor.commit();
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
alertDialog.setView(layout);
alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
}
public void showpickAlertDialog1(Context context, String title, String message) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
}
}
public类AddItemizedOverlay扩展ItemizedOverlay{
私有ArrayList mapOverlays=新ArrayList();
数据库处理程序数据库;
私人语境;
双lat;
双离子;
//警报对话框管理器
AlertDialogManager alert=新建AlertDialogManager();
公共AddItemizedOverlay(可绘制默认标记){
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(可绘制的defaultMarker,上下文){
这(默认标记);
this.context=上下文;
}
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView)
{
if(event.getAction()==1){
GeoPoint GeoPoint=mapView.getProjection().fromPixels(
(int)event.getX(),
(int)event.getY();
db=新数据库处理程序(上下文);
//纬度和经度(双倍)
lat=geopoint.getLatitudeE6()/1E6;
lon=地质点。getLongitudeE6()/1E6;
}
返回false;
}
@凌驾
受保护的OverlayItem createItem(int i){
返回mapOverlays.get(i);
}
@凌驾
公共整数大小(){
返回mapOverlays.size();
}
@凌驾
受保护的布尔onTap(整数索引){
OverlayItem item=mapOverlays.get(索引);
最终字符串标题=item.getTitle();
最后一个字符串片段=item.getSnippet();
if(title.equalsIgnoreCase(“您的位置”)){
showpickAlertDialog1(上下文、标题、代码段);
}
else if(title.equalsIgnoreCase(“0”)| | title.equalsIgnoreCase(“2”)| | title.equalsIgnoreCase(“3”)){
String lati=String.valueOf(lat);
String longi=String.valueOf(lon);
showpickAlertDialog2(上下文、纬度、经度、片段);
}
else if(title.equalsIgnoreCase(“1”)| title.equalsIgnoreCase(“4”)| title.equalsIgnoreCase(“5”)| title.equalsIgnoreCase(“6”)){
}
返回true;
}
公共void addOverlay(OverlayItem overlay){
mapOverlays.add(叠加);
}
公屋{
这个。填充();
}
}
定制的AlertDialogManager类
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
DatabaseHandler db;
private Context context;
double lat;
double lon;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
db = new DatabaseHandler(context);
// latitude & longtitude (double)
lat = geopoint.getLatitudeE6() / 1E6;
lon = geopoint.getLongitudeE6() / 1E6;
}
return false;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
final String title= item.getTitle();
final String snippet= item.getSnippet();
if(title.equalsIgnoreCase("Your Location")){
alert.showpickAlertDialog1(context, title, snippet);
}
else if(title.equalsIgnoreCase("0")||title.equalsIgnoreCase("2")||title.equalsIgnoreCase("3")){
String lati = String.valueOf(lat);
String longi = String.valueOf(lon);
alert.showpickAlertDialog2(context, lati, longi, snippet);
}
else if(title.equalsIgnoreCase("1")||title.equalsIgnoreCase("4")||title.equalsIgnoreCase("5")||title.equalsIgnoreCase("6")){
}
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
public class AlertDialogManager {
private Context context;
DatabaseHandler db = new DatabaseHandler(context);
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
// Showing Alert Message
alertDialog.show();
}
public void showpickAlertDialog2(final Context context, final String lat, final String lon,final String snippet) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
final Button park_button = new Button(context);
park_button.setHint("Park here");
// park_button.setBackgroundResource();
layout.addView(park_button);
park_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("parkbutton:Clicked");
// Delete DB Values
int count = db.getDatasCount();
for(int i = 0;i<count;i++){
db.deleteValues(i);
}
//latitude & longtitude (string)
String latitude = lat;
String longtitude = lon;
Log.d("Insert: ", "Inserting ..");
db.addData(new Datas(latitude, longtitude));
// Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
// Reading DB Data
Log.d("Reading: ", "Reading all Values...");
List<Datas> datas = db.getAllDatas();
for (Datas dat : datas) {
String log = "Id: "+dat.getID()+" ,Latitude: " + dat.getlat() + " ,Longtitude: " + dat.getlon();
// Writing DB data to log
Log.d("DB Data: ", log);
}
park_button.setEnabled(false);
}
});
Button know_more_button = new Button(context);
know_more_button.setHint("Know more");
// park_button.setBackgroundResource();
layout.addView(know_more_button);
know_more_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_REFERENCE", snippet);
editor.commit();
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
alertDialog.setView(layout);
alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
}
public void showpickAlertDialog1(Context context, String title, String message) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
}
}
公共类AlertDialogManager{
私人语境;
DatabaseHandler db=新的DatabaseHandler(上下文);
public void showAlertDialog(上下文上下文、字符串标题、字符串消息、,
布尔状态){
AlertDialog AlertDialog=新建AlertDialog.Builder(context.create();
//设置对话框标题
alertDialog.setTitle(标题);
//设置对话框消息
alertDialog.setMessage(消息);
如果(状态!=null)
//设置警报对话框图标
alertDialog.setIcon((状态)?R.drawable.success:R.drawable.fail);
//设置OK按钮
alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
系统出口(0);
}
});
//显示警报消息
alertDialog.show();
}
public void showpickAlertDialog2(最终上下文上下文、最终字符串lat、最终字符串lon、最终字符串片段){
AlertDialog AlertDialog=新建AlertDialog.Builder(context.create();
LinearLayout布局=新的LinearLayout(上下文);
布局。设置方向(线性布局。垂直);
最终按钮驻车按钮=新按钮(上下文);
停车按钮。设置提示(“在此停车”);
//park_按钮。setBackgroundResource();
布局。添加视图(驻车按钮);
park_button.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
System.out.println(“驻车按钮:点击”);
//删除数据库值
int count=db.getDatasCount();
对于(int i=0;i您当前正在向AlertDialogManager类中的DatabaseHandler
类构造函数传递空上下文:
public class AlertDialogManager {
private Context context;
DatabaseHandler db ;
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
this.context=context;
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
db = new DatabaseHandler(context);
//..your code here...
要关闭对话框,请使用alertDialog.disease()
而不是System.exit(0);
而不是Dialog.disease();尝试使用alertDialog.disease();该问题已得到纠正,另一个问题发现了空指针异常。您能详细告诉我哪一行吗?哦,我发现这是一个数据库错误。谢谢您与我共度的宝贵时间