选项菜单和上下文菜单

时间:2010-11-27 01:59:37

标签: android menu

我有listview fisrt时间我点击排它打开上下文菜单然后我覆盖oncontextitemselected

public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {

        case CALL_ID:
        {
        AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();

        String phone=mDbHelper.getPhone(info2.id);
        String toDial="tel:"+phone.toString();

        startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
        return true;
        }
    }
    return super.onContextItemSelected(item);
}

这是正确的,但当我

public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case CALL_ID:
        {
        AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();

        String phone=mDbHelper.getPhone(info2.id);
        String toDial="tel:"+phone.toString();

        startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
        return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
}

应用程序崩溃可以告诉我它们之间的区别

1 个答案:

答案 0 :(得分:2)

试试这个:

context_menu.xml (res / menu / context_menu.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/call"
          android:title="CALL" />
</menu>

上下文菜单:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }   
 @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.call: 

            String phone="555-555-555";
            String toDial="tel:"+phone.toString();

            Uri uri = Uri.parse(toDial);
            Intent it = new Intent(Intent.ACTION_DIAL, uri);  
            startActivity(it);  

        return true;

        default:
            return super.onContextItemSelected(item);
        }
    }

<强>的AndroidManifest.xml

<uses-permission  android:name="android.permission.CALL_PHONE"></uses-permission>

这应该有效。