2015-06-23 85 views
-1

//HappChat.javaTabhost內片段不工作

public class HappChat extends Fragment { 

TabHost tabhost; 

@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.chat_header, container, false); 

    tabhost = (TabHost) rootView.findViewById(android.R.id.tabhost); 

    tabhost.setup(); 

    TabSpec ts = tabhost.newTabSpec("Tab 1"); 
    ts.setIndicator("", getResources().getDrawable(R.drawable.tab_user)); 
    ts.setContent(new Intent(getActivity(), ChatFirstTab.class)); 
    tabhost.addTab(ts); 

    TabSpec ts1 = tabhost.newTabSpec("Tab 2"); 
    ts1.setIndicator("", getResources().getDrawable(R.drawable.tab_chat)); 
    ts1.setContent(new Intent(getActivity(), ChatSecondTab.class)); 
    tabhost.addTab(ts1); 

    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
    { 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E8E8E8")); 
    } 
    tabhost.getTabWidget().setCurrentTab(0); 
    tabhost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#FFFFFF")); 

    tabhost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      // TODO Auto-generated method stub 
      for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
      { 
       tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E8E8E8")); 
      } 

      tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); 
     } 
    }); 

    return rootView; 
    } 
} 

//ChatFirstTab.java

public class ChatFirstTab extends Fragment { 

@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.chat_first, container, false); 

    return rootView; 
    } 
} 

//ChatSecondTab.java

public class ChatSecondTab extends Fragment { 

@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.chat_first, container, false); 

    return rootView; 
    } 
} 

我的問題是,運行時上面的代碼顯示了下面描述的錯誤。

06-23 04:32:00.658: E/AndroidRuntime(12802): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 
06-23 04:32:00.658: E/AndroidRuntime(12802): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:754) 
06-23 04:32:00.658: E/AndroidRuntime(12802): at android.widget.TabHost.setCurrentTab(TabHost.java:420) 
06-23 04:32:00.658: E/AndroidRuntime(12802): at android.widget.TabHost.addTab(TabHost.java:247) 
06-23 04:32:00.658: E/AndroidRuntime(12802): at com.happwall.HappChat.onCreateView(HappChat.java:31) 
06-23 04:32:00.658: E/AndroidRuntime(12802): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870) 
etc... 

如何解決這個錯誤。我嘗試了很多,並提到更多的鏈接,但沒有成功。有沒有解決這個錯誤的方法。

任何幫助,將不勝感激。

+0

這是不可能的'Fragment' –

+0

嘗試加入'mTabHost.setup(getActivity(),getChildFragmentManager(),R.id.realtabcontent);' –

+0

@MD:那麼如何加載佈局內容是什麼? –

回答

0

你試過看這個post

它正在處理類似的問題。