0

我有一个可点击ListView并尝试为项目制作一个上下文菜单,但如果我第一次长按它会激活onListItemClick方法。下次它工作。如果我用它激活上下文菜单openContextMenu会抛出一个StackOverflowError.

我解决了我的问题!见文末。

我的活动:

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.search);

    getWords(getIntent());      
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id); 
        String lemma = (item.get(position));

        Intent intent = new Intent(this, EntryActivity.class)
            .putExtra(CL.DOC, doc)
            .putExtra(CL.ENTRY, lemma);
    startActivity(intent);
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Eintrag");
    getMenuInflater().inflate(R.menu.context, menu);
}

public boolean onContextItemSelected(MenuItem item) {
    //TODO: switch
    return super.onContextItemSelected(item);
}

private void getWords(Intent intent) {
    doc = (CLDocument)intent.getSerializableExtra(CL.DOC);
    item = doc.getEntries();

    ArrayAdapter<String> fileList = 
        new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {    
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            registerForContextMenu(view);
            //openContextMenu(view);        
            return false;
        }
    });
}

我的布局(search.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edit_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:singleLine="true" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/nowords"
        />

</LinearLayout>

编辑:如果我激活,这是 logcat openContextMenu

04-17 11:31:06.103: D/dalvikvm(279): GC_FOR_MALLOC freed 5691 objects / 348744 bytes in 65ms
04-17 11:31:08.553: I/dalvikvm(279): threadid=1: stack overflow on call to Landroid/widget/AbsListView;.showContextMenuForChild:ZL
04-17 11:31:08.553: I/dalvikvm(279):   method requires 36+20+24=80 bytes, fp is 0x42089340 (64 left)
04-17 11:31:08.564: I/dalvikvm(279):   expanding stack end (0x42089300 to 0x42089000)
04-17 11:31:08.564: I/dalvikvm(279): Shrank stack (to 0x42089300, curFrame is 0x4208beb8)
04-17 11:31:08.573: D/AndroidRuntime(279): Shutting down VM
04-17 11:31:08.573: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 11:31:08.703: D/dalvikvm(279): GC_FOR_MALLOC freed 7370 objects / 493888 bytes in 50ms
04-17 11:31:08.733: E/AndroidRuntime(279): FATAL EXCEPTION: main
04-17 11:31:08.733: E/AndroidRuntime(279): java.lang.StackOverflowError
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showC
04-17 11:31:08.823: D/dalvikvm(279): GC_FOR_MALLOC freed 3648 objects / 486736 bytes in 50ms

我改变了方法getWordsonCreateContextMenu现在它可以工作了:

private void getWords(Intent intent) {
    doc = (CLDocument)intent.getSerializableExtra(CL.DOC);
    item = doc.getEntries();

    ArrayAdapter<String> fileList = 
            new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   

    registerForContextMenu(getListView());

//      getListView().setOnItemLongClickListener(new OnItemLongClickListener() {    
//          @Override
//          public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//              registerForContextMenu(view);
//                      
//              return false;
//          }
//      });


}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info = 
            (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(item.get(info.position));
    getMenuInflater().inflate(R.menu.context, menu);

}

所以感谢所有试图帮助我的人!

4

2 回答 2

1

您必须在活动的回调中将registerForContextMenu您的ListView作为参数调用。onCreate()

于 2013-04-16T22:19:43.557 回答
0

这里:getListView().setOnItemLongClickListener(...)长按后注册上下文菜单(registerForContextMenu(view)),所以下次才可以使用。但是,如果您希望上下文菜单从头开始工作,您应该更早地调用它,例如在该registerForContextMenu(getListView())方法之前调用它。

于 2013-04-16T22:25:51.987 回答