سؤال

So, I was reading this earlier question for ideas on how to allow me to click an item in a list to do one action or long-press that item to switch to an ActionMode where I can select multiple items and use the ActionBar to do something to those items. However, I'm having issues with this answer. Specifically, I'm implementing this into a SherlockListFragment (using ActionBarSherlock). However, the moment I declare a new MultiChoiceModeListener, Eclipse throw up a couple of compile errors.

Description Resource    Path    Location    Type
Cannot override the final method from SherlockListFragment  DateTimeListFragment.java   /path/to/my/project line 127    Java Problem
The method inflate(int, Menu) in the type MenuInflater is not applicable for the arguments (int, Menu)  DateTimeListFragment.java   /path/to/my/project line 125    Java Problem

These go away the moment I remove the MultiChoiceModeListener. I have no idea what could be causing it, as there's nothing odd going on that I'm aware of.

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        //super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.alarmsmenu, menu); //line 125
    }
    public boolean onOptionsItemSelected(MenuItem Item) //line 127
    {
        switch(Item.getItemId())
        {
        case R.id.addAlarm:
            addAlarm();
            return true;
        case R.id.editAlarms:
            return true;
        default:
            return super.onOptionsItemSelected(Item);
        }       
    }

I'm very confused. Why does implementing MultiChoiceModeListener mean I can't override OnOptionsItemSelected?

EDIT: To help clarify, here are my imports.

import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TimePicker;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.app.ActionBar; //Yes, it's unused...
import com.actionbarsherlock.view.*;
import com.commonsware.cwac.loaderex.acl.*;
هل كانت مفيدة؟

المحلول

As a kind person from reddit has notified me, apparently ActionBarSherlock does not currently support MultiChoiceModeListener. The fact that I'm using ActionBarSherlock's menus when the listener wants native Android menus probably contributes to the issue as well.

نصائح أخرى

If you're using Eclipse, I'd delete all of your import directives, and hit Ctrl + Shift + o ( Cmd + Shift + o for Mac ), and VERY carefully choose imports based off of the conflicts.

My current SherlockActivites, that admittedly don't use fragments are doing:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

I did have the exact issues you were describing when I was converting my Activitys to SherlockActivitys.

EDIT: Adding the MultiChoiceModeListener interface to my class caused methods like the following to be generated:

public boolean onActionItemClicked( ActionMode mode, android.view.MenuItem item )
{
  // TODO Auto-generated method stub
  return false;
}

Note that the MenuItem is fully qualified. This may be a clue here. Perhaps you're trying to pass a Sherlock Menu to something that expects an Android Menu.

I guess my advice would be to try to qualify all of your possibly conflicting calls, and after you figure out which methods are being incorrectly called, go from there.

For example, change:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)

To:

public void onCreateOptionsMenu(Fully.Qualified.Path.Menu menu, MenuInflater inflater)

Make sure you are importing these two classes and not the vanilla android versions:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

Also, I'd try importing the ActionBarSherlock demo project and see if the ActionItem example works there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top