1

Trying to use startActionMode in fragment but got this error at

mMode = startActionMode(new AnActionModeOfEpicProportions()); LINE 55

http://pastebin.com/d9jzg9UC

How to slove this? ActionMode not supported in Fragment?

Thanks

xAnGz
  • 127
  • 4
  • 11

2 Answers2

4

You are trying to invoke an instance method of an Activity from a static Fragment (i.e. one that is separate from the instance of the Activity). Remembrer that you can always access the activity that contains your fragment using getActivity(). So:

  if (getActivity() != null) {
    mMode = getActivity().startActionMode(new AnActionModeOfEpicProportions());
  }

A cleaner approach would be to declare an interface class to manage the communication between fragments and activities.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Thanks for your reply,but before I post at SO, I did try `getActivity()` and also `Context` but of them both don't works, image : http://i.imgur.com/YRrCU.png – xAnGz Sep 29 '12 at 15:21
  • That's because you can't use "this" as an argument. You need to provide an instance of `ActionMode.Callback`, like in [this example](http://www.vogella.com/articles/AndroidListView/article.html#listview_actionbar) – dmon Sep 29 '12 at 23:33
  • The [documentation](http://developer.android.com/reference/android/app/Activity.html#startActionMode(android.view.ActionMode.Callback)) is your friend. – dmon Sep 29 '12 at 23:34
  • 5
    Thanks for your help. After hours of trying and searching. Found the answer over [here](http://stackoverflow.com/a/12507967/1039582), need to use `getSherlockActivity()` can't use `getActivity()` – xAnGz Sep 30 '12 at 12:12
0

in support library you can use it :

((ActionBarActivity) getActivity()).startSupportActionMode (new AnActionModeOfEpicProportions());
Vahid.Ahani
  • 124
  • 3
  • 10