The method startSupportActionMode(ActionMode.Callb

2019-02-11 01:02发布

问题:

I'm want to implement Action Mode Compact in Fragment Android. But I get error The method startSupportActionMode(ActionMode.Callback) is undefined for the type FragmentActivity in my code :

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    getActivity().startSupportActionMode(modeCallBack);

                    return true;
                }
        });

get error at getActivity().startSupportActionMode(modeCallBack);how to fix it ?

回答1:

From this ActionBarActivity startSupportActionMode (ActionMode.Callback callback) is method of ActionBarActivity which is used for ActionBar

if your Activity extend ActionBarActivity you can set like .

For ActionBarActivity

ActionBarActivity activity=(ActionBarActivity)getActiivty();
activity.startSupportActionMode(modeCallBack);

For Activity

getActivity().startActionMode(modeCallBack);


回答2:

try this

 list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, 
   int position, long id) {

  ((AppCompatActivity) getActivity()).startSupportActionMode(mActionModeCallback);

                return true;
            }
    });


回答3:

add call to setHasOptionsMenu(true); in

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);

}