2011-11-14 26 views
5

Tôi đang cố triển khai Chế độ xem tab cho hoạt động của mình. Tôi nhận được một lỗi mà nói.Lỗi tabView Android?

java.lang.IllegalArgumentException: bạn phải xác định một cách để tạo ra các chỉ số tab

 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, MonthActivity.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("Month") 
         .setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(this, WeekActivity.class); 
     spec = tabHost.newTabSpec("Week") 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, DayActivity.class); 
     spec = tabHost.newTabSpec("Day") 
       .setContent(intent); 
     tabHost.addTab(spec); 
     tabHost.setCurrentTab(2); 

Đây là xml của tôi và tôi đã tuyên bố hoạt động trong biểu hiện.

<?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:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    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" > 
     </FrameLayout> 
</LinearLayout> 

đâu tôi làm sai lầm của tôi? Cảm ơn thời gian và đầu vào của bạn.

Trả lời

8

thử điều này:

Resources res = getResources(); // Resource object to get Drawables 
tabHost = getTabHost(); // The activity TabHost 
TabHost.TabSpec spec; // Resusable TabSpec for each tab  
Intent intent; // Reusable Intent for each tab 

intent = new Intent().setClass(this, First.class);  
spec = tabHost.newTabSpec("First Tab").setIndicator("First Tab",res.getDrawable(R.drawable.icon)).setContent(intent); 
tabHost.addTab(spec); 

intent = new Intent().setClass(this, Second.class); 
spec = tabHost.newTabSpec("Second Tab").setIndicator("Second Tab",res.getDrawable(R.drawable.icon)).setContent(intent); 
tabHost.addTab(spec); 

tabHost.setCurrentTab(1); 

và chắc chắn rằng, hoạt động của bạn phải là một TabActivity nơi bạn xác định TabHost của bạn.

4

Câu trả lời rõ ràng và chính xác hơn là bạn chưa gọi phương thức setIndicator trên đối tượng TabHost.TabSpec, vì vậy nếu bạn thêm cuộc gọi này vào mọi đối tượng spec tab, sự cố của bạn sẽ được giải quyết.

spec.setIndicator ("Tab 1");