Tôi có một lớp để tạo hộp thoại và mã hóa để nhận các giá trị từ nó. nó hoạt động tốt cho một. và khi tôi cố gắng gọi điện thoại lần thứ hai, nó sẽ chuyển sau thông báo lỗi.Trẻ được chỉ định đã có cha/mẹ. Bạn phải gọi removeView() trên phụ huynh của đứa trẻ trước tiên
: java.lang.IllegalStateException: Trẻ được chỉ định đã có cấp độ gốc. Bạn phải gọi removeView() trên phụ huynh của đứa trẻ trước.
Bạn có thể vui lòng cho tôi biết cách xóa removeView() không?
đây là mã cho lớp học;
package com.util;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.EditText;
/**
* helper for Prompt-Dialog creation
*/
public abstract class PromptDialog extends AlertDialog.Builder implements OnClickListener {
private final EditText input;
/**
* @param context
* @param title resource id
* @param message resource id
*/
public PromptDialog(Context context, int title, int message) {
super(context);
setTitle(title);
//:TODO Display msg only if not empty
//setMessage(message);
input = new EditText(context);
setView(input);
setPositiveButton("ok", this);
setNegativeButton("cancel", this);
}
/**
* will be called when "cancel" pressed.
* closes the dialog.
* can be overridden.
* @param dialog
*/
public void onCancelClicked(DialogInterface dialog) {
dialog.dismiss();
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (onOkClicked(input.getText().toString())) {
dialog.dismiss();
}
} else {
onCancelClicked(dialog);
}
}
/**
* called when "ok" pressed.
* @param input
* @return true, if the dialog should be closed. false, if not.
*/
abstract public boolean onOkClicked(String input);
}
và đây là mã mà tôi gọi là phiên bản của lớp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final PromptDialog dlgName = new PromptDialog(this, R.string.enterName, R.string.enter_comment) {
@Override
public boolean onOkClicked(String input) {
// do something
mName = input;
save();
//end do some thing
return true; // true = close dialog
}
};
mTxtShiftName = (TextView) findViewById(R.id.shiftname);
mTxtShiftName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlgName.show();
}
});
Bạn có đang gọi lại hàm tạo hộp thoại trong lần thử thứ hai không? –
Tất cả mã tôi đang sử dụng đều được sao chép trong câu hỏi của tôi. Tôi nghĩ đó có thể là lý do. nhưng tôi không biết làm thế nào để tránh điều này? – SAN
Không gọi hàm khởi tạo hai lần khi bạn nhấp vào nút. Tạo hộp thoại của bạn một lần trong 'onCreate' bằng hàm tạo Dialog của bạn hoặc' onPrepareDailog' chỉ với mã, sau đó chỉ cần gọi dialog.show() khi bạn muốn hiển thị nó. –