问题:
开发android应用,用tabhost + ViewPager 实现左右滑动页面效果。只要是顺时或逆时,无论点击tab或者手势滑动都没有问题,问题是:如果跨一个或者多个tab点击,就会出错,强行退出应用。
以下是主要代码,当跨tab点击时,onTabChanged()这个方法有错,比如当前是第一个tab,然后点击第三个tab,这时代码mPager.setCurrentItem(2);这个出错!
请各位指教!
public class MainActivity extends Activity { TabHost mTabHost; @SuppressWarnings("deprecation") LocalActivityManager manager; @SuppressWarnings("deprecation") String newsTag = "T1"; String starTag = "T2"; String carmeTag = "T3"; String buyTag = "T4"; String setTag = "T5"; // ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。 // android-support-v4.jar ViewPager mPager;//页卡内容 private List<View> listViews; // Tab页面列表 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content); mTabHost = (TabHost) findViewById(android.R.id.tabhost); manager = new LocalActivityManager(MainActivity.this, false); manager.dispatchCreate(savedInstanceState); mTabHost.setup(manager); mTabHost.setOnTabChangedListener(new tabListener()); Intent newsIntent = new Intent(MainActivity.this, NewsFragmentActivity.class); Intent starIntent = new Intent(MainActivity.this, StarFragmentActivity.class); Intent camerIntent = new Intent(MainActivity.this, CameraActivity.class); Intent buyIntent = new Intent(MainActivity.this, BuyActivity.class); Intent settingIntent = new Intent(MainActivity.this, SettingActivity.class); mPager = (ViewPager) findViewById(R.id.viewpager); listViews = new ArrayList<View>(); listViews.add(manager.startActivity(newsTag, newsIntent).getDecorView()); listViews.add(manager.startActivity(starTag, starIntent).getDecorView()); listViews.add(manager.startActivity(carmeTag, camerIntent).getDecorView()); listViews.add(manager.startActivity(buyTag, buyIntent).getDecorView()); listViews.add(manager.startActivity(setTag, settingIntent).getDecorView()); // mPager.setAdapter(new MyPagerAdapter(listViews)); mPager.setCurrentItem(0); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); mTabHost.addTab(mTabHost .newTabSpec(newsTag) .setContent(newsIntent) .setIndicator(getResources().getString(R.string.news), getResources().getDrawable(R.drawable.tab_icon_news))); mTabHost.addTab(mTabHost .newTabSpec(starTag) .setContent(starIntent) .setIndicator(getResources().getString(R.string.star), getResources().getDrawable(R.drawable.tab_icon_news))); mTabHost.addTab(mTabHost .newTabSpec(carmeTag) .setContent(camerIntent) .setIndicator(getResources().getString(R.string.camera), getResources().getDrawable(R.drawable.tab_icon_news))); mTabHost.addTab(mTabHost .newTabSpec(buyTag) .setContent(buyIntent) .setIndicator(getResources().getString(R.string.buy), getResources().getDrawable(R.drawable.tab_icon_news))); mTabHost.addTab(mTabHost .newTabSpec(setTag) .setContent(settingIntent) .setIndicator(getResources().getString(R.string.set), getResources().getDrawable(R.drawable.tab_icon_news))); // mTabHost.setCurrentTab(0); updateTab(mTabHost); // InitViewPager(manager); } @SuppressWarnings("deprecation") @Override protected void onPause() { manager.dispatchPause(isFinishing()); super.onPause(); } @SuppressWarnings("deprecation") @Override protected void onResume() { manager.dispatchResume(); super.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("tab", mTabHost.getCurrentTabTag()); } class tabListener implements TabHost.OnTabChangeListener { @Override public void onTabChanged(String tabId) { updateTab(mTabHost); if(newsTag.equals(tabId)) { mPager.setCurrentItem(0); } else if(starTag.equals(tabId)) { mPager.setCurrentItem(1); } else if(carmeTag.equals(tabId)) { mPager.setCurrentItem(2); } else if(buyTag.equals(tabId)) { mPager.setCurrentItem(3); } else if(setTag.equals(tabId)) { mPager.setCurrentItem(4); } } } private void InitViewPager(final LocalActivityManager manager) { mPager = (ViewPager) findViewById(R.id.viewpager); listViews = new ArrayList<View>(); // LayoutInflater mInflater = getLayoutInflater(); // listViews.add(mInflater.inflate(R.layout.red_module_news, null)); // listViews.add(mInflater.inflate(R.layout.red_module_star, null)); // listViews.add(mInflater.inflate(R.layout.red_module_camera, null)); // listViews.add(mInflater.inflate(R.layout.red_module_buy, null)); // listViews.add(mInflater.inflate(R.layout.red_module_setting, null)); // System.out.println("hello"); // System.out.println(new MyPagerAdapter(listViews)); mPager.setAdapter(new MyPagerAdapter(listViews)); mPager.setCurrentItem(0); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); } /** * ViewPager适配器 */ public class MyPagerAdapter extends PagerAdapter { public List<View> mListViews; public MyPagerAdapter(List<View> mListViews) { this.mListViews = mListViews; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(mListViews.get(arg1)); } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { return mListViews.size(); } @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(mListViews.get(arg1)); return mListViews.get(arg1); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } } /** * 页卡切换监听 */ public class MyOnPageChangeListener implements OnPageChangeListener { @Override public void onPageSelected(int arg0) { mTabHost.setCurrentTab(arg0); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } } }
报错信息:
10-28 10:53:00.876: E/AndroidRuntime(3776): FATAL EXCEPTION: main
10-28 10:53:00.876: E/AndroidRuntime(3776): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.ViewGroup.addView(ViewGroup.java:3306)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.addView(ViewPager.java:1111)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.ViewGroup.addView(ViewGroup.java:3251)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.ViewGroup.addView(ViewGroup.java:3227)
10-28 10:53:00.876: E/AndroidRuntime(3776): at com.red.main.MainActivity$MyPagerAdapter.instantiateItem(MainActivity.java:239)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.populate(ViewPager.java:821)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:469)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:441)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:422)
10-28 10:53:00.876: E/AndroidRuntime(3776): at com.red.main.MainActivity$tabListener.onTabChanged(MainActivity.java:186)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:463)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.widget.TabHost.setCurrentTab(TabHost.java:448)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:563)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.View.performClick(View.java:4354)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.view.View$PerformClick.run(View.java:17948)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.os.Handler.handleCallback(Handler.java:725)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.os.Looper.loop(Looper.java:137)
10-28 10:53:00.876: E/AndroidRuntime(3776): at android.app.ActivityThread.main(ActivityThread.java:5293)
10-28 10:53:00.876: E/AndroidRuntime(3776): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 10:53:00.876: E/AndroidRuntime(3776): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 10:53:00.876: E/AndroidRuntime(3776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-28 10:53:00.876: E/AndroidRuntime(3776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-28 10:53:00.876: E/AndroidRuntime(3776): at dalvik.system.NativeStart.main(Native Method)
"你应该贴出错误信息及代码。######<div class=""ref"">
左右滑动只要viewpager就行了,要tabhost干嘛?Tab的效果完全可以自己用几个Button实现,或者用ActionBar的Tab,现在ActionBar有低版本support包了,v7,2.1以上都可以用了
"版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。