【问题标题】:Tabhost inside Fragment not working片段内的 Tabhost 不起作用
【发布时间】:2015-06-23 08:49:18
【问题描述】:

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

如何解决此错误。我已经尝试了很多并引用了更多链接,但没有成功。有什么方法可以解决这个错误。

任何帮助将不胜感激。

【问题讨论】:

  • Fragment是不可能的
  • 尝试添加mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
  • @MD : 那么如何加载布局内容呢?

标签: java android android-tabhost


【解决方案1】:

你试过看这个post吗?

它正在处理类似的问题。

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多