1

当我长按 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)
4

1 回答 1

0

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

renameAlert = new AlertDialog.Builder(mContext);

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

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

于 2013-08-23T21:29:32.830 回答