gpt4 book ai didi

android - 在 onItemLongClick 中显示 AlertDialog 会给出有关父项的错误,我必须做什么?

转载 作者:行者123 更新时间:2023-11-30 01:40:33 25 4
gpt4 key购买 nike

我试图在用户执行 Listview 的 onItemLongClick 时显示一个对话框,以确认用户是否想要这样做,但在 adb.show( ):

The specified child already has a parent. You must call removeView() on the child's parent first

使用以下代码:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
adb.setView(view);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
} });

adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
//finish();
} });
adb.show();

return true;
}
});

我搜索了答案,但我没有看到有关 listview 或 longclick 的信息,所以我需要帮助 :(

添加 logcat,它给出错误的行是出现adb.show();的地方:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
at android.view.ViewGroup.addView(ViewGroup.java:3786)
at android.view.ViewGroup.addView(ViewGroup.java:3758)
at com.android.internal.app.AlertController.setupView(AlertController.java:492)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
at pt.isec.jogodememoria.MenuEscolheNivel$2.onItemLongClick(MenuEscolheNivel.java:119)
at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

一个很容易犯的错误,缺少 AlertDialog alertDialog = adb.create(); 并且不需要 setView。您仍然可以创建一个单独的方法并为每次长时间的项目点击实例化它。

listView.setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

createDialog(view);
return true;
}
});

然后为您的对话框创建一个单独的方法:

public void createDialog(View view){

AlertDialog.Builder adb = new AlertDialog.Builder(this);
//adb.setView(Main.this);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
} });

adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
//finish();
} });

AlertDialog alertDialog = adb.create();
alertDialog.show();

}

enter image description here

关于android - 在 onItemLongClick 中显示 AlertDialog 会给出有关父项的错误,我必须做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568576/

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