Fragment里面的Tabhost不工作

时间:2015-06-23 08:49:18

标签: java android android-tabhost

// HappChat.java

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...

如何解决此错误。我已经尝试了很多并提到了更多链接,但没有成功。有没有办法解决这个错误。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您是否尝试过查看此post

它正在处理类似的问题。