我正在编写一个应用程序来管理帐户和密码,一切 很好,只有按钮编辑有问题。我点击它一次,它工作正常,当我第二次点击它,它崩溃。你能帮助我吗? This is logcat
private ListView listview_account;
private Button add_new;
private EditText newtype, oldtype;
private EditText newusername, oldusername;
private EditText newpassword, oldpassword;
private List<Account> list_accounts;
private String[] list_type;
private MyDatabaseHelper helper = new MyDatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
listview_account = (ListView)findViewById(R.id.list_account);
add_new = (Button)findViewById(R.id.ADD);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView_addnew = inflater.inflate(R.layout.dialog_addnew, null);
final View dialogView_change = inflater.inflate(R.layout.dialog_change, null);
newtype = (EditText)dialogView_addnew.findViewById(R.id.type);
newusername = (EditText)dialogView_addnew.findViewById(R.id.username);
newpassword = (EditText)dialogView_addnew.findViewById(R.id.password);
oldtype = (EditText)dialogView_change.findViewById(R.id.type_old);
oldusername = (EditText)dialogView_change.findViewById(R.id.username_old);
oldpassword = (EditText)dialogView_change.findViewById(R.id.password_old);
update_show();
final AlertDialog.Builder builder_show = new AlertDialog.Builder(this);
final AlertDialog.Builder builder_add = new AlertDialog.Builder(this);
final AlertDialog.Builder builder_delete = new AlertDialog.Builder(this);
final AlertDialog.Builder builder_change = new AlertDialog.Builder(this);
builder_add.setView(dialogView_addnew);
builder_change.setView(dialogView_change);
builder_delete.setTitle("Delete Account");
builder_delete.setIcon(android.R.drawable.ic_delete);
builder_delete.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder_change.setTitle("Edit");
builder_change.setIcon(android.R.drawable.ic_input_get);
builder_change.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder_add.setTitle("Add new account");
builder_add.setIcon(android.R.drawable.ic_input_add);
builder_add.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
add_new_account();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}
}
);
builder_add.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
newtype.setText("");
newusername.setText("");
newpassword.setText("");
newtype.requestFocus();
}
}
);
final AlertDialog alert_add_new = builder_add.create();
add_new.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
alert_add_new.show();
}
}
);
listview_account.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v,final int pos, long id) {
builder_show.setTitle(list_type[pos]);
builder_show.setIcon(android.R.drawable.ic_dialog_info);
builder_show.setMessage("Username: " + list_accounts.get(pos).getUSERNAME()
+ "\nPassword: " + list_accounts.get(pos).getPASSWORD());
builder_show.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
builder_delete.setMessage("Do you want to remove " + list_type[pos]+ "?\nUsername: " + list_accounts.get(pos).getUSERNAME() + "\nPassword: " + list_accounts.get(pos).getPASSWORD());
builder_delete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
helper.DeleteAcc(list_type[pos]);
Toast.makeText(getApplicationContext(), "Success.", Toast.LENGTH_SHORT).show();
update_show();
}
});
final AlertDialog dialog_delete = builder_delete.create();
dialog_delete.show();
}
}
);
builder_show.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
oldtype.setText(list_type[pos]);
oldusername.setText(list_accounts.get(pos).getUSERNAME());
oldpassword.setText(list_accounts.get(pos).getPASSWORD());
builder_change.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
helper.DeleteAcc(list_type[pos]);
change();
}
});
AlertDialog alert_change = builder_change.create();
alert_change.show();
}
}
);
AlertDialog alert1_show = builder_show.create();
alert1_show.show();
}
});
}
private void add_new_account()
{
String new_type = newtype.getText().toString();
String new_username = newusername.getText().toString();
String new_password = newpassword.getText().toString();
Account new_acc = new Account(new_type, new_username, new_password);
helper.AddAccount(new_acc);
update_show();
newtype.setText("");
newusername.setText("");
newpassword.setText("");
newtype.requestFocus();
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
private void change()
{
String old_type = oldtype.getText().toString();
String old_username = oldusername.getText().toString();
String old_password = oldpassword.getText().toString();
Account new_acc = new Account(old_type, old_username, old_password);
helper.AddAccount(new_acc);
update_show();
Toast.makeText(getApplicationContext(), "Đã lưu.", Toast.LENGTH_SHORT).show();
}
private void update_show()
{
list_accounts = helper.getListAccount();
list_type = new String[list_accounts.size()];
int i = 0;
for (Account acc: list_accounts) { list_type[i++] = acc.TYPE; }
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_type);
listview_account.setAdapter(adapter);
}
答案 0 :(得分:0)
至于我,问题是您在builder_show
内创建了ItemClickListener
对话框。
listview_account.setOnItemClickListener(new AdapterView.OnItemClickListener()
实际上,您每次都会创建它,并且在第二次系统显示错误时,“指定的孩子已经有父母。您必须首先在孩子的父母上调用removeView()。”因为对话框已存在。
如果您希望保留所有内容,则应在每次show()调用之前删除对话框。查看this了解详情。
但正确(此问题)将在ItemClickListener
之外移动对话框创建代码。