I have a very simple app which is just an activity with a tab view on it.
I have initialised and casted everything to as it should be but am continually getting a nu
This Line shows that the TabHost you have created is in fragment_main.xml
inside layout directory which is used by PlaceholderFragment
tools:context="com.example.app.MainActivity$PlaceholderFragment"
But you are finding your TabHost in activity_main.xml
Move your code from onCreate()
to onCreateView
of PlaceholderFragment, below in same class if you are using default template comes with AS like
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1, spec2, spec3;
spec1 = tabHost.newTabSpec("spec1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab1");
tabHost.addTab(spec1);
spec2 = tabHost.newTabSpec("spec2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab2");
tabHost.addTab(spec2);
spec3 = tabHost.newTabSpec("spec3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab3");
tabHost.addTab(spec3);
return rootView;
}
Or If you want you can move your TabHost to activity_main.xml
as well without changing the java code but that is not recommended using fragments having a lot of benefits.
Check this for benefits of using Fragments
fragment_main and activity_main layouts in Android Studio
This my code to use TabHost,but now I use fragment instead.
public class MainActivity extends TabActivity {
private TabHost tabHost;
private TabHost.TabSpec spec;
@SuppressWarnings("unused")
private Resources res;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_new);
//Test Button
Button testBtn = (Button)findViewById(R.id.title_imagebtn);
testBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog();
}
});
this.res = getResources();
this.tabHost = getTabHost();
Intent intent = new Intent().setClass(this, LearnResActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.res_learn))
.setIndicator(
getString(R.string.res_learn),
getResources().getDrawable(
android.R.drawable.ic_media_play))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyLearnActivity.class);
this.spec = this.tabHost
.newTabSpec(getResources().getString(R.string.my_learn))
.setIndicator(
getString(R.string.my_learn),
getResources().getDrawable(
android.R.drawable.ic_menu_recent_history))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyTestActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.my_test))
.setIndicator(
getString(R.string.my_test),
getResources().getDrawable(
android.R.drawable.ic_menu_edit))
.setContent(intent);
this.tabHost.addTab(this.spec);
}}