android片段中的空对象引用错误

时间:2015-10-16 06:19:10

标签: android android-tabhost

公共类HomeActivity扩展了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);
}

}

上显示空对象引用
tabobj = (FragmentTabHost)findViewById(android.R.id.tabhost);   call

1 个答案:

答案 0 :(得分:0)

像这样创作

@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;
}