我正在尝试在viewpager中实现自定义标签。有人可以建议如何设置它。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.tabs_viewpager_layout);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
// Initialise the TabHost
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); }
this.intialiseViewPager();
mViewPager.setCurrentItem(2);
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
/**
* Initialise ViewPager
*/
private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Info.class.getName()));
fragments.add(Fragment.instantiate(this, Search.class.getName()));
fragments.add(Fragment.instantiate(this, Home.class.getName()));
fragments.add(Fragment.instantiate(this, Profile.class.getName()));
this.mPagerAdapter = new PagerAdapter(
super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
View view = createTabView(mTabHost.getContext(), "test");
TabsViewPagerFragmentActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab1").setIndicator(view),
(tabInfo = new TabInfo("Tab1", Info.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsViewPagerFragmentActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab2").setIndicator("",
getResources().getDrawable(R.drawable.search_icon)),
(tabInfo = new TabInfo("Tab2", Search.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsViewPagerFragmentActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab3").setIndicator("",
getResources().getDrawable(R.drawable.home_icon)),
(tabInfo = new TabInfo("Tab3", Home.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setCurrentTab(2);
TabsViewPagerFragmentActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab4").setIndicator("",
getResources().getDrawable(R.drawable.member_icon)),
(tabInfo = new TabInfo("Tab4", Profile.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width = (width / 4) - 50;
mTabHost.getTabWidget().getChildAt(1).getLayoutParams().width = (width / 4) - 50;
mTabHost.getTabWidget().getChildAt(2).getLayoutParams().width = (width / 4) + 100;
mTabHost.getTabWidget().getChildAt(3).getLayoutParams().width = (width / 4) - 50;
mTabHost.setOnTabChangedListener(this);
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(
R.layout.tab_indicator_holo, null);
TextView tv = (TextView) view.findViewById(R.id.title);
tv.setText(text);
return view;
}
private static void AddTab(TabsViewPagerFragmentActivity activity,
TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
这是我的堆栈跟踪
07-22 07:53:45.631: E/AndroidRuntime(1612): FATAL EXCEPTION: main
07-22 07:53:45.631: E/AndroidRuntime(1612): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.udebate/com.main.udebate.TabsViewPagerFragmentActivity}: java.lang.NullPointerException
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.os.Looper.loop(Looper.java:137)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 07:53:45.631: E/AndroidRuntime(1612): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 07:53:45.631: E/AndroidRuntime(1612): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 07:53:45.631: E/AndroidRuntime(1612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 07:53:45.631: E/AndroidRuntime(1612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 07:53:45.631: E/AndroidRuntime(1612): at dalvik.system.NativeStart.main(Native Method)
07-22 07:53:45.631: E/AndroidRuntime(1612): Caused by: java.lang.NullPointerException
07-22 07:53:45.631: E/AndroidRuntime(1612): at com.main.udebate.TabsViewPagerFragmentActivity.createTabView(TabsViewPagerFragmentActivity.java:244)
07-22 07:53:45.631: E/AndroidRuntime(1612): at com.main.udebate.TabsViewPagerFragmentActivity.initialiseTabHost(TabsViewPagerFragmentActivity.java:178)
07-22 07:53:45.631: E/AndroidRuntime(1612): at com.main.udebate.TabsViewPagerFragmentActivity.onCreate(TabsViewPagerFragmentActivity.java:121)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.Activity.performCreate(Activity.java:5104)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 07:53:45.631: E/AndroidRuntime(1612): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)