当我长按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();
}
});
这很好用,项目名称和其他所有内容都已成功更改,但它只适用于ONCE。当我重命名文件然后再次尝试重命名时,应用程序崩溃,这是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)
答案 0 :(得分:0)
用户Cruceo在评论中给了我答案。问题是我在onCreate中声明了一个新的AlertDialog.Builder,所以它是在第一次启动活动时创建的。所以我刚刚搬家了
renameAlert = new AlertDialog.Builder(mContext);
指向click方法,以便每次单击按钮时都会声明它并且它有效。
Cruceo:如果你发表自己的答案,我会接受你的。谢谢!