gpt4 book ai didi

java - 当第二次打开 AlertDialog 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 04:27:47 26 4
gpt4 key购买 nike

当我长按 ListView 项目时,它会打开一个带有三个按钮的 AlertDialog。其中之一是“重命名”以重命名项目等。

longClickAlert.setNegativeButton("Rename", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
renameAlert.setMessage("New name:");
renameAlert.setView(input);
renameAlert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
renameLine(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedFilesArray.get(toDelete), input.getText().toString());
currentFilePath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(toDelete) + ".mp3";
renameFile(input.getText().toString());
recordedFilesArray.set(toDelete, input.getText().toString());
listAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
renameAlert.show();
dialog.dismiss();
}
});

这工作正常,项目名称和其他所有内容都已成功更改,但它只能工作一次。当我重命名该文件然后尝试再次重命名任何内容时,应用程序崩溃,这是 logcat 文件:

08-21 11:26:13.631: E/AndroidRuntime(7260): FATAL EXCEPTION: main
08-21 11:26:13.631: E/AndroidRuntime(7260): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.view.ViewGroup.addViewInner(ViewGroup.java:3506)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.view.ViewGroup.addView(ViewGroup.java:3377)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.view.ViewGroup.addView(ViewGroup.java:3353)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.android.internal.app.AlertController.setupView(AlertController.java:413)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.android.internal.app.AlertController.installContent(AlertController.java:241)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.app.AlertDialog.onCreate(AlertDialog.java:337)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.app.Dialog.show(Dialog.java:260)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.whizzappseasyvoicenotepad.RecordedLibrary$3$2.onClick(RecordedLibrary.java:158)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.os.Looper.loop(Looper.java:137)
08-21 11:26:13.631: E/AndroidRuntime(7260): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 11:26:13.631: E/AndroidRuntime(7260): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 11:26:13.631: E/AndroidRuntime(7260): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 11:26:13.631: E/AndroidRuntime(7260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 11:26:13.631: E/AndroidRuntime(7260): at dalvik.system.NativeStart.main(Native Method)

最佳答案

用户 Cruceo评论里给了我答案。问题是我在 onCreate 中声明了一个新的 AlertDialog.Builder,因此它仅在第一次启动 Activity 时创建。所以我就搬家了

renameAlert = new AlertDialog.Builder(mContext);

到 click 方法,以便每次单击按钮时都声明它并且它起作用。

Cruceo:如果您发布自己的答案,我会接受您的。谢谢!

关于java - 当第二次打开 AlertDialog 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com