【发布时间】:2014-08-25 06:16:25
【问题描述】:
我从母亲的活动中得到一个片段。现在我想在这个片段中创建标签。但 tabHost.setup() 方法显示错误。我不明白,所以需要有关错误的线索。
// FragmentOne.java
package com.example.sharelocationui;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TabHost;
public class FragmentOne extends Fragment {
private FragmentTabHost tabHost;
public FragmentOne() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_layout_one, container,
false);
tabHost = (FragmentTabHost) v.findViewById(R.id.testtabhost1);
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(),getChildFragmentManager(),R.id.testtabhost1);
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("TAB1"));
tabHost.addTab(tabHost.newTabSpec("tab_test2").setIndicator("TAB2"));
tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("TAB3"));
tabHost.setCurrentTab(0);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
tabHost = null;
}
}
//fragment_layout_one
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testtabhost1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
显示错误
'The method setup(Context, FragmentManager, int) in the type FragmentTabHost is not applicable for the arguments (Activity, FragmentManager, int)'
【问题讨论】:
-
使用小部件 tabHost 而不是片段 tabHost....
-
但是 setup() 方法参数 s 中的问题是什么?
-
你是在扩展fragment而不是fragment Activity,你可以在fragment中创建tabHost而不是fragmentTabHost....所以只使用tabHost并将标签内容替换为fragment...
-
在 setup() 中添加这个 getActivity().getApplicationContext() 而不是 getActivity()
标签: android fragment android-tabs fragment-tab-host