2014-02-25 36 views
0

我想在標籤選擇打開新的一頁...... 當我啓動項目就突然死機推出新的一頁......Tabhost通過意向

這裏是源... (日誌錯誤是您的內容必須具有tabshost其id屬性是R.id.tabhost)

public class AddFriends_Activity extends TabActivity implements SwipeInterface { 
TabHost mTabHost; 
Resources res = getResources(); 
     Intent i = new Intent(this,Added_Friends.class); 
     Intent j = new Intent(this,Friend_List.class); 
     TabHost mTabHst = getTabHost(); 
mTabHst.addTab(mTabHst.newTabSpec("tab_test1").setIndicator("Add Frinds",res.getDrawable(R.drawable.add1)) 
       .setContent(i)); 

    mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator("Friend List",res.getDrawable(R.drawable.add1)) 
       .setContent(j)); 


     int tabCount = mTabHst.getTabWidget().getTabCount(); 
     for (int r = 0; r < tabCount; r++) { 
      final View view = mTabHst.getTabWidget().getChildTabViewAt(r); 
      if (view != null) { 
       // reduce height of the tab 
       view.getLayoutParams().height *= 0.90; 

       // get title text view 
       final View textView = view.findViewById(android.R.id.title); 
       if (textView instanceof TextView) { 

        ((TextView) textView).setGravity(Gravity.BOTTOM); 
        ((TextView) textView).setTextSize((float) 12); 
        ((TextView) textView).setTextColor(android.graphics.Color.WHITE); 

        ((TextView) textView).setSingleLine(false); 

        // explicitly set layout parameters 
        textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT; 
        textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; 
       } 
      } 
     } 

     mTabHst.setCurrentTab(0); 
+0

請張貼logcat的 –

+0

看到http://stackoverflow.com/questions/3272500/android-exception-did-you-forget-to-call-public-void-setup-localactivitymanag –

回答

0

下面是答案終於...

Resources res = getResources(); 
     Intent i = new Intent(this,Added_Friends.class); 
     Intent j = new Intent(this,Friend_List.class); 
     TabHost mTabHst = getTabHost(); 
     mTabHst.addTab(mTabHst.newTabSpec("tab_test1").setIndicator(null,res.getDrawable(R.drawable.ic_launcher)) 
       .setContent(i)); 

    mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator(null,res.getDrawable(R.drawable.ic_launcher)) 
       .setContent(j)); 


     int tabCount = mTabHst.getTabWidget().getTabCount(); 
     for (int r = 0; r < tabCount; r++) { 
      final View view = mTabHst.getTabWidget().getChildTabViewAt(r); 
      if (view != null) { 
       // reduce height of the tab 
       view.getLayoutParams().height *= 0.90; 

       // get title text view 
       final View textView = view.findViewById(android.R.id.title); 
       if (textView instanceof TextView) { 

        ((TextView) textView).setGravity(Gravity.CENTER); 
        ((TextView) textView).setTextSize((float) 12); 
        ((TextView) textView).setTextColor(android.graphics.Color.BLACK); 

        ((TextView) textView).setSingleLine(false); 

        // explicitly set layout parameters 
        textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT; 
        textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; 
       } 
      } 
     } 

     mTabHst.setCurrentTab(0); 

下面添加XML在佈局...

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="5dip"> 


    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_below="@+id/tabcontent" 
     android:layout_gravity="center" 
     android:gravity="center" /> 
     <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@android:id/tabs" 
     android:layout_marginBottom="60dp"> 

    </FrameLayout> 
</RelativeLayout>