public void AddCustomEvent() {
ListView lv = (ListView) findViewById(R.id.listView1);
final Set<String> tasks = sp.getStringSet("tasks", new HashSet<String>());
ArrayList<String> taskarr = new ArrayList<String>();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", String.valueOf(position) + " " + String.valueOf(id));
final int pos = position;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set title
alertDialogBuilder.setTitle("Do you want to delete this task");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to delete !")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Set<String> tasks = sp.getStringSet("tasks", new HashSet<String>());
final ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayList<String> list = new ArrayList<String>();
for (String str : tasks) {
list.add(str);
}
list.remove(pos);
Set<String> newTasks = new HashSet<String>();
for (String str : list) {
newTasks.add(str);
}
Editor edit = sp.edit();
edit.putStringSet("tasks", newTasks);
edit.commit();
final StableArrayAdapter adapter = new StableArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
我制作了一个任务应用。它在删除任务之前显示alertdialog。没什么特别的。我的代码因为'崩溃(我想要显示alertdialog)而被破坏。
有人检查代码并告诉我泄漏服务的方式和地点。
答案 0 :(得分:0)
堆栈跟踪中没有提到您的类。除非您实际使用包名称编写应用程序,例如:com.android.emailcommon.utility。但我想你不会在这里问问题:)