5

내 활동을 위해 탭보기를 구현하려고합니다. 상태에 오류가 있습니다.Android TabView 오류가 발생 했습니까?

java.lang.IllegalArgumentException가 : 당신은 탭 표시

 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); 

이 내 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: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> 

는 어디서 실수는 무엇입니까? 시간과 노력에 감사드립니다.

답변

8

이 시도 :

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); 

을하고 있는지 확인하십시오, 당신의 활동은 당신이 당신의 TabHost를 정의한 TabActivity해야합니다.

4

더 명확하고 정확한 대답은 TabHost.TabSpec 객체에서 setIndicator 메서드를 호출하지 않았기 때문에이 호출을 모든 탭 사양 객체에 추가하면 문제가 해결된다는 것입니다.

spec.setIndicator ("Tab 1");

관련 문제