6

我有一个扩展 SherlockActivity 的类,因为我在活动中使用了 ActionBarSherlock 操作栏。但是,我还尝试将上下文菜单添加到此活动中的列表视图,该活动在列表视图中的项目被长按时启动。我的声明中有一个错误,onContextItemSelected(MenuItem item)表明我的覆盖无效,因为没有什么可以覆盖。但是,当我删除覆盖时,永远不会调用该函数。我知道这与 SherlockActivity 从 Activity 继承的方式有关,但我不确定具体情况。也许我错过了进口?在此处查看相关代码:

public class Inbox extends SherlockActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inbox);
    refreshMsgs(this.getCurrentFocus());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId() == R.id.lstInbox){
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        String contactName = lstConversation.get(info.position).getContactName();   
        menu.setHeaderTitle(contactName);
        menu.add(Menu.NONE, 0, 0, "View contact");
        menu.add(Menu.NONE, 1, 1, "Delete thread");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    SmsConversation selectedConvo = lstConversation.get(info.position);         

    if (item.getItemId() == 0){
        //view contact
        Intent viewcontactIntent = new Intent();
        String contactId = selectedConvo.getContactId();
        Uri uriContact = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
        viewcontactIntent.setData(uriContact);
        PendingIntent pViewIntent = PendingIntent.getActivity(getBaseContext(), 1, viewcontactIntent, 0);
        try {
            pViewIntent.send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
    } else {
      //DELETE thread
    }
    return true;
}
}

以下是我在课堂上的所有导入:

import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Html;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
4

1 回答 1

23

很久以前面对类似的问题,我已经onContextItemSelected()通过强制MenuItem使用其完全限定的类名 ( android.view.MenuItem) 来覆盖,如下所示:

@Override
public boolean onContextItemSelected (android.view.MenuItem item)
{
  //implementation
}
于 2013-01-24T01:52:28.287 回答