Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing tabs with simple views and fragments
    text
    copied!<p>After one week struggle with common ways of implementing tabs like TabActivity and fragments and viewPager I failed to get a instance of Activities or fragments of tabs and couldn't find a way to solve the issue. So I decided to implement it in a different way. First I make a tabWidget with a simple button. In android developer website I find out a way to replace fragment in runtime. so the only thing remains is that how to get access to tab fragments to invoke methods from my FragmentActivity. </p> <p>here is my FragmentActivity using pageViewer. I get nullpointer when I want to get fragment object of my tab in <code>setup()</code> method:</p> <pre><code>public class MainActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener { private TabHost mTabHost; private ViewPager mViewPager; private HashMap&lt;String, TabInfo&gt; mapTabInfo = new HashMap&lt;String, MainActivity.TabInfo&gt;(); private PagerAdapter mPagerAdapter; private TabInfo mLastTab = null; private class TabInfo { private String tag; private Class clss; private Bundle args; private Fragment fragment; TabInfo(String tag, Class clazz, Bundle args) { this.tag = tag; this.clss = clazz; this.args = args; } } class TabFactory implements TabContentFactory { private final Context mContext; /** * @param context */ public TabFactory(Context context) { mContext = context; } /** * (non-Javadoc) * * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String) */ public View createTabContent(String tag) { View v = new View(mContext); v.setMinimumWidth(0); v.setMinimumHeight(0); return v; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); Log.d("checking", "setup tabs..."); setContentView(R.layout.activity_main); // // initialiseTabHost(savedInstanceState); if (savedInstanceState != null) { mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); } intialiseViewPager(); // // setup(); // /// } protected void onSaveInstanceState(Bundle outState) { outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab // selected super.onSaveInstanceState(outState); } private void intialiseViewPager() { List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;(); fragments .add(Fragment.instantiate(this, CoachFragment.class.getName())); fragments .add(Fragment.instantiate(this, LogingFragment.class.getName())); fragments.add(Fragment.instantiate(this, HistoryFragment.class.getName())); this.mPagerAdapter = new PagerAdapter( super.getSupportFragmentManager(), fragments); // this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager); this.mViewPager.setAdapter(this.mPagerAdapter); this.mViewPager.setOnPageChangeListener(this); mViewPager.setOffscreenPageLimit(1000000); } private void initialiseTabHost(Bundle args) { mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); TabInfo tabInfo = null; View tabView1 = createTabView(this, coach"); MainActivity.AddTab(this, this.mTabHost, this.mTabHost .newTabSpec("Tab1").setIndicator(tabView1), (tabInfo = new TabInfo("Tab1", CoachFragment.class, args))); this.mapTabInfo.put(tabInfo.tag, tabInfo); View tabView2 = createTabView(this, logbook"); MainActivity.AddTab(this, this.mTabHost, this.mTabHost .newTabSpec("Tab2").setIndicator(tabView2), (tabInfo = new TabInfo("Tab2", LogingFragment.class, args))); this.mapTabInfo.put(tabInfo.tag, tabInfo); View tabView3 = createTabView(this, "history"); MainActivity.AddTab(this, this.mTabHost, this.mTabHost .newTabSpec("Tab3").setIndicator(tabView3), (tabInfo = new TabInfo("Tab3", HistoryFragment.class, args))); this.mapTabInfo.put(tabInfo.tag, tabInfo); mTabHost.setOnTabChangedListener(this); } private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { // Attach a Tab view factory to the spec tabSpec.setContent(activity.new TabFactory(activity)); tabHost.addTab(tabSpec); } public void setup() { .... CoachFragment fragment=(CoachFragment) mPagerAdapter.getRegisteredFragment(mViewPager.getCurrentItem()); fragment.setTempView(R.id.welcome); fragment.animate(); .... } public void onTabChanged(String tag) { // TabInfo newTab = this.mapTabInfo.get(tag); int pos = this.mTabHost.getCurrentTab(); this.mViewPager.setCurrentItem(pos); } private static View createTabView(final Context context, final String text) { View view = LayoutInflater.from(context) .inflate(R.layout.tabs_bg, null); TextView tv = (TextView) view.findViewById(R.id.tabsText); tv.setText(text); return view; } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int position) { // TODO Auto-generated method stub this.mTabHost.setCurrentTab(position); } </code></pre> <p>}</p> <p>my pageAdapter:</p> <pre><code>public class PagerAdapter extends FragmentPagerAdapter { private List&lt;Fragment&gt; fragments; private HashMap&lt;Integer, Fragment&gt; registeredFragments=new HashMap&lt;Integer, Fragment&gt;(); /** * @param fm * @param fragments */ public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) { super(fm); this.fragments = fragments; } /* (non-Javadoc) * @see android.support.v4.app.FragmentPagerAdapter#getItem(int) */ @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); registeredFragments.put(position, fragment); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { registeredFragments.remove(position); super.destroyItem(container, position, object); } public Fragment getRegisteredFragment(int position) { return registeredFragments.get(position); } /* (non-Javadoc) * @see android.support.v4.view.PagerAdapter#getCount() */ @Override public int getCount() { return this.fragments.size(); } </code></pre> <p>}</p> <p>and my LogingFragment. the other 2 fragments are exactly the same one:</p> <pre><code>public class LogingFragment extends Fragment{ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // We have different layouts, and in one of them this // fragment's containing frame doesn't exist. The fragment // may still be created from its saved state, but there is // no reason to try to create its view hierarchy because it // won't be displayed. Note this is not needed -- we could // just run the code below, where we would create and return // the view hierarchy; it would just never be used. return null; } return (LinearLayout)inflater.inflate(R.layout.coach_activity, container, false); } </code></pre> <p>}</p> <p>Every Thing works perfect and all looking and swiping works fine but in setup() method I get nullPointer. please help me with it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload