0

public class HomeActivity extends FragmentActivity {

// Fragment TabHost as tabobj
private FragmentTabHost tabobj;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Set the Window to Full Screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_home);


    tabobj = (FragmentTabHost)findViewById(android.R.id.tabhost);


    tabobj.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
  //  tabobj.setup(HomeActivity.this, getSupportFragmentManager(), R.id.realtabcontent);

    tabobj.addTab(tabobj.newTabSpec("tab1").setIndicator("ALL"),
            Tab1Fragment.class, null);
    tabobj.addTab(tabobj.newTabSpec("tab2").setIndicator("INCOME"),
            Tab2Fragment.class, null);
    tabobj.addTab(tabobj.newTabSpec("tab3").setIndicator("EXPENSE"),
            Tab3Fragment.class, null);
}

}

Shows null object reference on

tabobj = (FragmentTabHost)findViewById(android.R.id.tabhost);   call
therealprashant
  • 701
  • 15
  • 27
  • is `tabhost` exist in `activity_home.xml`? – Kishore Jethava Oct 16 '15 at 06:25
  • tabhost not exist in activity_home.xml its accessed from android os (default),its call by (android.R.id.tabhost) –  Oct 16 '15 at 06:28
  • look at [this](https://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/) . you have to create `FragmentTabHost` inside xml – Kishore Jethava Oct 16 '15 at 06:34
  • Sorry itz shows the same error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artificers.subin.testtab/com.artificers.subin.testtab.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentTabHost.setup(android.content.Context, android.support.v4.app.FragmentManager, int)' on a null object reference –  Oct 16 '15 at 06:46
  • You have to create it in `activity_home.xml` before you can reference it in your code. Otherwise it will stay null. – panonski Oct 16 '15 at 06:53
  • Cannot have to import thim manually. File > Project Structure > app (under “Modules) > Dependencies tab. Click the “+” at the bottom to add a Library Dependency; in this case “support-v4“. @kishore jethava –  Oct 16 '15 at 09:42

1 Answers1

0

create like this

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container,Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     tabobj= new FragmentTabHost(getActivity());
        tabobj.setup(getActivity(), getChildFragmentManager(), R.id.container);

        tabobj.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                MyProfileFragment.class, null);
        tabobj.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                Tab1Fragment.class, null);
        tabobj.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                Tab2Fragment.class, null);
        tabobj.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                Tab2Fragment.class, null);
    return tabobj;
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
  • tabobj.setup(getActivity(), getChildFragmentManager(), R.id.container); shows error –  Oct 16 '15 at 09:44
  • replace `container` with `realtabcontent` if `container` not resolved. or post your `activity_home.xml` so i can guide you – Kishore Jethava Oct 16 '15 at 09:47