-1

I'm creating a swipeable Sherlock tab. I used this tutorial to do it:

Android ActionBarSherlock ViewPager Tabs Tutorial

However when I want to start an activity from inside of a fragment (for example FragmentTab1) I need context to create intent and start activity. I'm in the Fragment class and don't have access to context! I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class. What should I do?!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
i'm Ahmad
  • 67
  • 13
  • 1
    come on man really, you keep fragments inside Activity not the other way around – Pankaj Nimgade May 01 '15 at 13:14
  • please read the link and then write a comment man!!! :( – i'm Ahmad May 01 '15 at 13:36
  • with all due respect, kindly have Toolbar (v7) with SlidingTabLayout it would certainly save you time and give you enough flexibility, https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html – Pankaj Nimgade May 01 '15 at 13:47
  • I know. but I want to hide put actionbar and have tab except actionbar. Sherlock is for this purpose . – i'm Ahmad May 01 '15 at 14:20

2 Answers2

1

Sherlock Fragments works with getSherlockActivity() instead of getActivity() You should try using

getSherlockActivity().startActivity(...)

  • getSherlockActivity() is good when I start a fragment from sherlockFragmentActivity, not from FragmentPagerAdapter! – i'm Ahmad May 01 '15 at 12:29
0

I'm in the Fragment class and don't have access to context!

Yes, you do. Call getActivity() to return the Activity that is hosting this fragment. Activity inherits from Context.

I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.

So? That does not somehow magically cause the getActivity() method to vanish.

For some reason, you are using ActionBarSherlock, which has been deprecated for about 20 months by its author. If your fragment is a SherlockFragment, you will want to call getSherlockActivity() instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • can you read the link I put in my post and change FragmentTab1 and start an sample activity on it?! – i'm Ahmad May 01 '15 at 13:29
  • @user3212159: There is nothing in that tutorial that would be a trigger to start an activity (action bar item, button, etc.). You may wish to edit your question and explain, **completely and precisely**, what you mean by "start an sample activity on it". – CommonsWare May 01 '15 at 13:41
  • suppose we have an activity(myActivity for example). I should start activity this way: `startActivity(new Intent(getSherlockActivity() , myActivity.class));` . but it does not work and returns exception error; – i'm Ahmad May 01 '15 at 14:25
  • @user3212159: Use LogCat to examine the Java stack trace associated with your exception: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand what the stack trace is telling you, ask a fresh Stack Overflow question where you paste in all relevant code, plus the stack trace, to try to get assistance. – CommonsWare May 01 '15 at 14:31
  • Getting - java.lang.IllegalStateException: No tab known for tag null when trying to launch Intent inside fragment, please help. – Ashish Tamrakar Jul 01 '15 at 03:48