Bonjour tout le monde.
Dans mon application, j’ai essayé de mettre un TabHost dans un autre Tabhost. eclipse n’affiche aucune erreur mais j’ai un message d’erreur qui s’affiche lors de l’execution au niveau de l’émulateur : «Unfortunately, Application has stopped ».
Voici le code JAVA du premier Tabhost qui contient 3 onglets :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class CustomTabsActivity extends TabActivity{
	private TabHost tabHost;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTabs();
	}
 
	private void setTabs() {
		tabHost = getTabHost();
 
		addTab(R.string.tab_1, R.drawable.tab_info);
		addTab(R.string.tab_2, R.drawable.tab_info);
		addTab(R.string.tab_3, R.drawable.tab_info);
	}
 
	private void addTab(int labelId, int drawableId) {
		Intent intent = new Intent(this, MockActivity.class);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);		
 
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
 
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);
 
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
 
	}
}
Et le fichier XML associé :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/tab_space_top" >
 
    <RelativeLayout
        android:id="@+id/tab_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
        <TabWidget
            android:id="@+id/tabs"
            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"
            android:layout_below="@android:id/tabs" >
        </FrameLayout>
    </RelativeLayout>
 
</TabHost>
et le code java du 2 éme TabHost :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MockActivity extends TabActivity {
    //on declare le TabHost comme variable statique afin qu’il soit accessible pour tout le monde  
   public static TabHost tabHost;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mock);
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        TabSpec homeTabSpec = tabHost.newTabSpec("tid1");
        Intent mainActivity = new Intent(this, MainActivity.class);
        Drawable d = getResources().getDrawable(R.drawable.setting_onclick);
        ImageView img1 = new ImageView(this);
        img1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        img1.setPadding(20, 10, 20, 10);
        img1.setImageDrawable(d);
        homeTabSpec.setIndicator(img1).setContent(mainActivity);
        tabHost.addTab(homeTabSpec);
	/*la même chose pour les autres tabwidget*/
    }
}
Et enfin le fichier XML associé :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#777777" >
 
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
 
            <RelativeLayout
                android:id="@+id/layTab"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerVertical="true"
                android:background="@drawable/btm_bar"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp" >
 
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true" />
            </RelativeLayout>
 
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@id/layTab"
                android:layout_alignParentTop="true" />
        </RelativeLayout>
    </TabHost>
</LinearLayout>
Merci pour votre aide.