gpt4 book ai didi

android - 尝试在长按列表项时添加对话框,导致 StackOverflow 错误

转载 作者:行者123 更新时间:2023-11-29 14:33:11 24 4
gpt4 key购买 nike

当我使用 longClick(标准点击打开项目详细信息)时,我想添加一个带有 2 个按钮的对话框 editremove(实际上只删除实现)

所以我尝试按照 this link 上的指南进行操作

我已经添加了

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.edit_dialog, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();

switch (item.getItemId()) {
case R.id.remove_item:

final String[] listItem = getItemId();

String idItemSel = listItem[info.position];

delItem(idItemSel);
adapter.notifyDataSetChanged();
return true;
}
return false;
}

(其中 adapter 是我的列表适配器)

并且我已经以这种方式配置了监听器

list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
.....

}
});


list.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
view.showContextMenu();
return true;

}
});

一切正常,但是当我使用长按时,应用程序崩溃并出现此错误(在调用 view.showContextMenu(); 时):

08-02 18:13:12.079: E/AndroidRuntime(7780): FATAL EXCEPTION: main
08-02 18:13:12.079: E/AndroidRuntime(7780): java.lang.StackOverflowError
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1995)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onItemLongClick(MyItems.java:615)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2001)
08-02 18:13:12.079: E/AndroidRuntime(7780): at android.view.View.showContextMenu(View.java:2520)
08-02 18:13:12.079: E/AndroidRuntime(7780): at detailer.myapp.it.MyItems$6.onIt

当我长按列表项时,是否有任何解决问题的方法或查看简单上下文对话框的替代方法?

最佳答案

您似乎已经为 ContextMenu 注册了您的 ListView 并且设置了一个调用 showContextMenu() 的 OnItemLongClickListener,这创建了一个循环逻辑:

  • OnItemLongClickListener 调用 ContextMenu,
  • 上下文菜单调用 OnItemLongClickListener,
  • OnItemLongClickListener 调用 ContextMenu,
  • 上下文菜单调用 OnItemLongClickListener,
  • 等直到发生堆栈溢出

解决这个问题:

首先,删除 OnItemLongClickListener,因为它与 ContextMenu 是多余的。

其次,仔细检查您如何使用上下文菜单。您已将菜单保存在布局文件夹中,如果您已经使用过 registerForContextMenu(),则无需调用 showContextMenu()。


要设置上下文菜单,只需将整个 ListView 传递给 registerForContextMenu():

registerForContextMenu(list);

您需要这样的 XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/edit"
android:title="@string/edit" />
<item
android:id="@+id/remove"
android:title="@string/remove" />
</menu>

将其保存在新文件夹 res/menu 中,我将其命名为 context_menu.xml。

然后更改创建上下文菜单的方法:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}

现在,长按时每一行都会自动打开一个上下文菜单。


如果您在编辑和删除行方面需要帮助,请发布您用于创建 ListView 和适配器的代码。

关于android - 尝试在长按列表项时添加对话框,导致 StackOverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781692/

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