Tabhost throwing up a runtime error - code seems fine to me - but obviously not. I have 2 activities that I want to show in some tabs.
Error
(java.lang.IllegalArgumentException: you must specify a way to create the tab content)
MainActivity
public class MainActivity extends TabActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab 1
TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
// setting Title and Icon for the Tab
tab1.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab1Intent = new Intent(this, Tab1Activity.class);
tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab1.setContent(tab1Intent);
// Tab 2
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
// setting Title and Icon for the Tab
tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab2Intent = new Intent(this, Tab2Activity.class);
tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab1.setContent(tab2Intent);
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dawnlp.innertab.MainActivity"
android:background="#00547d"
android:textColor="#FFFFFF"
>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Dont think it has anything to do with the other activities I have.
change last tab code as :
You are using tab1 instead of tab2 for indicator
// Tab 2
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
// setting Title and Icon for the Tab
tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab2Intent = new Intent(this, Tab2Activity.class);
tab2Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab2.setContent(tab2Intent);
Related
I am trying to create a tab host to but I cant seem to make it work, what am i doing wrong? I have setup up createTab() on my onCreate method but it crashes.
I am simply creating a tabhost that holds around 5 tabs. Also I am trying to implement swipe view if anyone knows how to implement it
Main activity
public class MainActivity extends Activity {
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void createTab() {
TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
tab1Spec.setIndicator("TAB 1");
Intent p = new Intent(this, tab1.class);
tab1Spec.setContent(p);
TabHost.TabSpec tab2Spec = tab.newTabSpec("tab2");
tab2Spec.setIndicator("TAB 2");
Intent p = new Intent(this, tab2.class);
tab2Spec.setContent(p);
tab.addTab(tab1Spec);
tab.addTab(tab2Spec);
}
}
Activity_main xml
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#+id/tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
You didn't initialize your tab host , then you've used it in this line which throw a NullPointerException :
TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
before using tab initialize it like :
tab = (TabHost)this.findViewById(android.R.id.tabhost)
I have a button and below i have tabview with several tabs.
Wherewer I choose a tab I want to listen clics for elements on main activity. For example how can I listen clicks for imRefresh?
If I set onitem click listener I recieve nullpointer exception
main activity, which ititialise tabview
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, Tab1Activity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Test1")
.setIndicator("Test1")
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, Tab2Activity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Test2")
.setIndicator("Test2")
.setContent(intentApple);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
layout of main activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/for_t"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/imRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_toLeftOf="#+id/imageView3"
android:src="#drawable/ic_action_refresh" />
<ImageView
android:id="#+id/imSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_search" />
</RelativeLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="yarh.youtubenblog.MainActivity"
tools:ignore="MergeRootFrame" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
</LinearLayout>
one of tabs activity
public class Tab2Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView refresh = (ImageView) findViewById(R.id.imRefresh);
refresh.setOnClickListener(refreshV);
TextView textview = new TextView(this);
textview.setText("This is Apple tab");
setContentView(textview);
}
private OnClickListener refreshV = new OnClickListener() {
#Override
public void onClick(View v) {
GetAdds feed = new GetAdds();
feed.execute();
}
};
}
You can't - period. Not even via hacking around with ViewParents and window hierarchies. Sorry. This is due to how TabActivity works: Each of its tabs is actually its own self-contained Activity, that gets kind of "transplanted" into the TabActivity via some magic on the Android Framework's side. Separate activities have absolutely no way (via Android APIs) of getting view information about each other.
What'd make your task a lot simpler, is to switch to Fragments. Your use case is one of the most common examples: check out http://android.codeandmagic.org/android-tabs-with-fragments/
I have a strange error with my code.
I am trying to show to user logo and after that to display the tabs.
Tried to clean and build project still the same errors,and I dont use import R.
But my problem is some strange error :
java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
My app have error on
setContentView(R.layout.logoscreen);
my code is:
public class tabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.logoscreen);
new GetDataTask(this).execute();
}
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
Context context;
GetDataTask(Context context){this.context=context;}
protected void onPostExecute(Integer result) {
//tabs.this.setContentView(R.layout.tabs);
setContentView(R.layout.logoscreen);
//setContentView(R.layout.tabs);
TabHost tabHost= (TabHost)tabs.this.findViewById(R.id.tabhost);
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(context,start.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Heb news").setIndicator("Heb news").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(context, rusNewsP.ListRusNews.class);
spec = tabHost.newTabSpec("Rus News").setIndicator("Rus News").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
my xml file is logoscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
thanks for the help!!!
if you use Tabhost then must have android:id="#android:id/tabhost" and You have
android:id="#+id/tabhost"
I am developing an Android application in which I have a tab bar. I want that when I click on tab new class should open. How do I implement this? My main problem is when I pass the intent on clicking the tab. Then it's crashing.
I am posting my Java:
package com.solataire;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tab_host = (TabHost) findViewById(R.id.tab_host);
tab_host.setup();
TabSpec tabspec1 = tab_host.newTabSpec("TAB_1");
tabspec1.setIndicator("Tab 1");
tabspec1.setContent(R.id.first_tab);
tab_host.addTab(tabspec1);
TabSpec tabspec2 = tab_host.newTabSpec("TAB_2");
tabspec2.setIndicator("Tab 2");
tabspec2.setContent(R.id.second_tab);
tab_host.addTab(tabspec2);
TabSpec tabspec3 = tab_host.newTabSpec("TAB_3");
tabspec3.setIndicator("Tab 3");
tabspec3.setContent(R.id.third_tab);
tab_host.addTab(tabspec3);
tab_host.setCurrentTab(0);
}
}
My XML file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost android:id="#+id/tab_host"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#android:id/tabs"
android:layout_gravity="bottom" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout android:id="#+id/first_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="First Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/second_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="Second Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/third_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="One More Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/edit_item_text_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</TabHost>
</LinearLayout>
Can anyone help me in this point?
You can try this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
This is the xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Hi I'm following tutorial provided by google name hello-tabwidget.
To create tab menu.
Everything works fine but now I want to add a button to one tab but
this button appears in all tabs.
Please can anyone help?
Thanks
this is what i have
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstTab.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First Tab").setIndicator("First Tab",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SecondTab.class);
spec = tabHost.newTabSpec("Second Tab").setIndicator("Second Tab",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdTab.class);
spec = tabHost.newTabSpec("Third Tab").setIndicator("Third Tab",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NextTab.class);
spec = tabHost.newTabSpec("Next Tab").setIndicator("Next Tab",
res.getDrawable(R.drawable.ic_tab_next))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<include layout="#layout/tab1"/>
<include layout="#layout/tab2"/>
<include layout="#layout/tab3"/>
<include layout="#layout/tab4"/>
</FrameLayout>
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"/>
</LinearLayout>
</TabHost>
I created xml layout for each tab this is one with the button
others are exactly this same just with out a button tag and with different id
tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tab2Layout"
android:orientation="vertical">
<Button android:text="#+id/Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
and i created class for each tab this is code from second tab where i want to have a button
the other classes are exactly this same just
setContentView(R.layout.tab2);
is set to point to different layouts
SecondTab.java
public class SecondTab extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
}
Any ideas ??
Thanks
solved it!!!
in main.xml i included those 4 lines:
<include layout="#layout/tab1"/>
<include layout="#layout/tab2"/>
<include layout="#layout/tab3"/>
<include layout="#layout/tab4"/>
those lines shouldn't be there
so main.xml looks like that now:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
</FrameLayout>
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"/>
</LinearLayout>
</TabHost>