我从母亲活动中得到一个片段。现在我想在这个片段中创建标签。但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)'
答案 0 :(得分:2)
您的问题是您正在导入
android.app.Fragment
不
android.support.v4.app.Fragment
答案 1 :(得分:2)
这非常适合您的问题
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.comfortstay.R;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View v = inflater.inflate(R.layout.fragment_tab_layout, container, false);
tabLayout = (TabLayout) v.findViewById(R.id.tabs);
viewPager = (ViewPager) v.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return v;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new DashboardFragment();
case 1:
return new DashboardFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
String recent_news = "My Order";
return recent_news;
case 1:
String category = "Popular Items";
return category;
}
return null;
}
}
}
和布局是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tab_background_color"
android:elevation="4dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/tabIndicatorColor"
app:tabIndicatorHeight="5dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/tabSelectedTextColor"
app:tabTextColor="@color/tabTextColor"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>
答案 2 :(得分:0)
在onCreateView方法中,只需更改并删除
即可View v = inflater.inflate(R.layout.fragment_layout_one, container,
false);
return v;
到
return tabhost
更新:
您尚未初始化FragmentTabHost。
mTabHost = new FragmentTabHost(getActivity());
供参考检查official example
答案 3 :(得分:-1)
但是在导入android.support.v4.app.Fragment时如何调用片段。
它是MainActivity.java的一部分:
register m128 c = compare_ge(b, a);
c = -c;
c &= a;
b -= c;
它是HelloTab.java的一部分:
Button b = (Button) findViewById(R.id.buttonGoTab);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = null;
FragmentManager fragmentManager =getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragment= new HelloTab(); //Here is error
fragmentTransaction.replace(android.R.id.content, fragment).commit();
}
});