我的项目中,为了内容丰富,想在fragment中实现4个切换的Tab,正好遇到viewPageIndicater,于是按照网上给出的教程操作并在Actvity实现了切换的效果,可是当我用同样的方法用在fragment时却未能如愿:
代码是直接copy进去的,当时在mAdapter = new TabAdapter_chart(getSupportFragmentManager());这句话时getSupportFragmentManager()有报错,没有太去在意,把它用getFragmentManager();而后面出现的问题恰恰就出在这个不经意的改动上
之后在运行程序的时候,程序启动到主界面,点击其他任何页面的back键就会将整个程序kill掉,之前都是好好的;
java.lang.RuntimeException: Unable to resume activity {com.example.administrator.moniter_app/com.example.administrator.moniter_app.MainActivity}: java.lang.IllegalStateException: Observer android.support.v4.view.ViewPager$PagerObserver@432d0780 was not registered.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2782)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2811)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5049)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Observer android.support.v4.view.ViewPager$PagerObserver@432d0780 was not registered.
at android.database.Observable.unregisterObserver(Observable.java:69)
at android.support.v4.view.PagerAdapter.unregisterDataSetObserver(PagerAdapter.java:294)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:413)
at com.example.administrator.moniter_app.Chart_fragment.initView(Chart_fragment.java:54)
at com.example.administrator.moniter_app.Chart_fragment.onResume(Chart_fragment.java:45)
at android.support.v4.app.Fragment.performResume(Fragment.java:1832)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:419)
at com.example.administrator.moniter_app.Chart_fragment.initView(Chart_fragment.java:54)
at com.example.administrator.moniter_app.Chart_fragment.onResume(Chart_fragment.java:45)
at android.support.v4.app.Fragment.performResume(Fragment.java:1832)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1939)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:447)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:436)
at android.app.Activity.performResume(Activity.java:5341)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2772)
我在主界面中就有用到viewpager,反复核对发现是在使用viewpageIndicater那个页面,这会才想起来之前随意改的那个地方,getSupportFragmentManeger是啥意思啊
最后才发现,然来fragment中应该使用getChildFragmentManeger();
不要一直只copy代码,而不理解意思啊