Android - FragmentTransaction奇怪的行为

时间:2014-11-11 16:26:13

标签: android android-activity fragment fragmenttransaction fragmentmanager

FragmentTransaction trans = mFragmentManager.beginTransaction();
if (animate) {
    trans.setCustomAnimations(R.anim.slide_fragment_in_on_replace, R.anim.slide_fragment_out_on_replace);
}
trans.replace(containers.get(tabPosition).getId(), fragment);
trans.commit();

当我在onCreate Activity中调用此功能时应用程序崩溃了,但是如果我在调用它trans.commit();时删除onCreate则可行。我测试了添加延迟100ms然后上面的代码工作,但我不想要延迟(丑陋的解决方法,可能适用于一些快速设备)。

我用谷歌搜索了#34;找不到片段ID的视图"是的,我使用:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

我得到的以下堆栈跟踪是:

E/FragmentManager( 2386): No view found for id 0x7f070018 (se.appname.android:id/my_tab_1) for fragment CheckInWebFragment{52dd567c #0 id=0x7f070018}
E/FragmentManager( 2386): Activity state:
D/FragmentManager( 2386):   Local FragmentActivity 52c25174 State:
D/FragmentManager( 2386):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
D/FragmentManager( 2386):     mLoadersStarted=false
D/FragmentManager( 2386):   Active Fragments in 52c259c8:
D/FragmentManager( 2386):     #0: CheckInWebFragment{52dd567c #0 id=0x7f070018}
D/FragmentManager( 2386):       mFragmentId=#7f070018 mContainerId=#7f070018 mTag=null
D/FragmentManager( 2386):       mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
D/FragmentManager( 2386):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
D/FragmentManager( 2386):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
D/FragmentManager( 2386):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
D/FragmentManager( 2386):       mFragmentManager=FragmentManager{52c259c8 in Main{52c25174}}
D/FragmentManager( 2386):       mActivity=se.appname.android.activities.Main@52c25174
D/FragmentManager( 2386):       mArguments=Bundle[{lastnames=null, requireNames=false}]
D/FragmentManager( 2386):   Added Fragments:
D/FragmentManager( 2386):     #0: CheckInWebFragment{52dd567c #0 id=0x7f070018}
D/FragmentManager( 2386):   FragmentManager misc state:
D/FragmentManager( 2386):     mActivity=se.appname.android.activities.Main@52c25174
D/FragmentManager( 2386):     mContainer=android.support.v4.app.FragmentActivity$2@52c25e4c
D/FragmentManager( 2386):     mCurState=2 mStateSaved=false mDestroyed=false
D/FragmentManager( 2386):   View Hierarchy:
D/FragmentManager( 2386):     com.android.internal.policy.impl.PhoneWindow$DecorView{52c27960 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):       com.android.internal.widget.ActionBarOverlayLayout{52c27d38 V.E..... ... 0,0-0,0 #102033d android:id/action_bar_overlay_layout}
D/FragmentManager( 2386):         android.widget.FrameLayout{52c28324 V.E..... ... 0,0-0,0 #1020002 android:id/content}
D/FragmentManager( 2386):           android.widget.RelativeLayout{52d7cf04 V.E..... ... 0,0-0,0 #7f070054 app:id/main_layout}
D/FragmentManager( 2386):             android.widget.LinearLayout{52d7d1cc V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):               se.appname.android.views.tabs.TabViewPager{52d7de58 VFED.... ... 0,0-0,0 #7f070055 app:id/pager}
D/FragmentManager( 2386):             android.widget.RelativeLayout{52d81e70 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):               android.widget.LinearLayout{52d8214c V.E..... ... 0,0-0,0 #7f070056 app:id/lay_tab_indicator_container}
D/FragmentManager( 2386):                 android.widget.LinearLayout{52d82358 G.E..... ... 0,0-0,0 #7f070057 app:id/lay_tab_indicator}
D/FragmentManager( 2386):                   se.appname.android.views.tabs.TabPageIndicator{52d82af0 VFED.... ... 0,0-0,0 #7f070058 app:id/tab_indicator}
D/FragmentManager( 2386):                     android.widget.LinearLayout{52d83474 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                       se.appname.android.views.tabs.TabView{52d87834 VFE...C. .S. 0,0-0,0}
D/FragmentManager( 2386):                         android.widget.RelativeLayout{52d88d90 V.E..... .S. 0,0-0,0}
D/FragmentManager( 2386):                           android.widget.ImageView{52d89090 V.ED.... .S. 0,0-0,0 #7f070123 app:id/image_view}
D/FragmentManager( 2386):                           android.widget.TextView{52d8aa44 V.ED.... .S. 0,0-0,0 #7f07021e app:id/text_view}
D/FragmentManager( 2386):                       se.appname.android.views.tabs.TabView{52d8b0ac VFE...C. ... 0,0-0,0}
D/FragmentManager( 2386):                         android.widget.RelativeLayout{52d8b5d4 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                           android.widget.ImageView{52d8b8b0 V.ED.... ... 0,0-0,0 #7f070123 app:id/image_view}
D/FragmentManager( 2386):                           android.widget.TextView{52d8bab0 V.ED.... ... 0,0-0,0 #7f07021e app:id/text_view}
D/FragmentManager( 2386):                       se.appname.android.views.tabs.TabView{52d8d4ac VFE...C. ... 0,0-0,0}
D/FragmentManager( 2386):                         android.widget.RelativeLayout{52d8d9d4 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                           android.widget.ImageView{52d8dcb0 V.ED.... ... 0,0-0,0 #7f070123 app:id/image_view}
D/FragmentManager( 2386):                           android.widget.TextView{52d8deb0 V.ED.... ... 0,0-0,0 #7f07021e app:id/text_view}
D/FragmentManager( 2386):                       se.appname.android.views.tabs.TabView{52d8f8b0 VFE...C. ... 0,0-0,0}
D/FragmentManager( 2386):                         android.widget.RelativeLayout{52d8fdd8 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                           android.widget.ImageView{52d900b4 V.ED.... ... 0,0-0,0 #7f070123 app:id/image_view}
D/FragmentManager( 2386):                           android.widget.TextView{52d902b4 V.ED.... ... 0,0-0,0 #7f07021e app:id/text_view}
D/FragmentManager( 2386):                       se.appname.android.views.tabs.TabView{52d91cb8 VFE...C. ... 0,0-0,0}
D/FragmentManager( 2386):                         android.widget.RelativeLayout{52d921e0 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                           android.widget.ImageView{52d924bc V.ED.... ... 0,0-0,0 #7f070123 app:id/image_view}
D/FragmentManager( 2386):                           android.widget.TextView{52d926bc V.ED.... ... 0,0-0,0 #7f07021e app:id/text_view}
D/FragmentManager( 2386):                 android.widget.FrameLayout{52d83774 V.E..... ... 0,0-0,0 #7f070049 app:id/loading_indicator_container}
D/FragmentManager( 2386):                   android.widget.LinearLayout{52d8395c V.E..... ... 0,0-0,0 #7f07004a app:id/loading_indicator}
D/FragmentManager( 2386):                     android.widget.ProgressBar{52d83c24 V.ED.... ... 0,0-0,0 #7f07004b app:id/loading_indicator_progressbar}
D/FragmentManager( 2386):                     android.widget.TextView{52d84ad4 V.ED.... ... 0,0-0,0 #7f07004c app:id/loading_indicator_text}
D/FragmentManager( 2386):               android.widget.LinearLayout{52d84cf4 G.E..... ... 0,0-0,0 #7f07005a app:id/top_main_view}
D/FragmentManager( 2386):         android.widget.LinearLayout{52c28508 V.E..... ... 0,0-0,0 #102033e android:id/top_action_bar}
D/FragmentManager( 2386):           com.android.internal.widget.ActionBarContainer{52c28714 V.ED.... ... 0,0-0,0 #102033f android:id/action_bar_container}
D/FragmentManager( 2386):             com.android.internal.widget.ActionBarView{52c29bdc V.E..... ... 0,0-0,0 #1020340 android:id/action_bar}
D/FragmentManager( 2386):               android.widget.LinearLayout{52c2efb0 VFE...C. ... 0,0-0,0}
D/FragmentManager( 2386):                 com.android.internal.widget.ActionBarView$HomeView{52c2fb64 V.E..... ... 0,0-0,0}
D/FragmentManager( 2386):                   android.widget.ImageView{52c2fd58 V.ED.... ... 0,0-0,0 #1020252 android:id/up}
D/FragmentManager( 2386):                   android.widget.ImageView{52c307fc V.ED.... ... 0,0-0,0 #102002c android:id/home}
D/FragmentManager( 2386):               android.widget.LinearLayout{52c207b8 V.E..... ... 0,0-0,0 #7f07004d app:id/lay_view_swicher}
D/FragmentManager( 2386):                 se.appname.android.views.generic.ScandinavianBlackTextView{52c23580 V.ED..C. ... 0,0-0,0 #7f070050 app:id/txt_actionbar_title}
D/FragmentManager( 2386):                 android.widget.ImageButton{52a39728 VFED..C. ... 0,0-0,0 #7f070051 app:id/triangle_icon}
D/FragmentManager( 2386):             com.android.internal.widget.ActionBarContextView{52c322b0 G.E..... ... 0,0-0,0 #1020341 android:id/action_context_bar}
D/FragmentManager( 2386):           android.widget.ImageView{52c32544 V.ED.... ... 0,0-0,0}
D/FragmentManager( 2386):         com.android.internal.widget.ActionBarContainer{52c32910 G.ED.... ... 0,0-0,0 #1020342 android:id/split_action_bar}
D/AndroidRuntime( 2386): Shutting down VM
W/dalvikvm( 2386): threadid=1: thread exiting with uncaught exception (group=0xa4bcf648)
W/GAV2    ( 2386): Thread[main,5,main]: dispatch call queued.  Need to call GAServiceManager.getInstance().initialize().
D/HockeyApp( 2386): Writing unhandled exception to: /data/data/se.appname.android/files/6117be88-c224-454e-bed3-eea08ed07d56.stacktrace
E/AndroidRuntime( 2386): FATAL EXCEPTION: main
E/AndroidRuntime( 2386): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.appname.android/se.appname.android.activities.Main}: java.lang.IllegalArgumentException: No view found for id 0x7f070018 (se.appname.android:id/my_tab_1) for fragment CheckInWebFragment{52dd567c #0 id=0x7f070018}
E/AndroidRuntime( 2386):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime( 2386):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime( 2386):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 2386):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 2386):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2386):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 2386):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 2386):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2386):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 2386):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 2386):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 2386):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2386): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f070018 (se.appname.android:id/my_tab_1) for fragment CheckInWebFragment{52dd567c #0 id=0x7f070018}
E/AndroidRuntime( 2386):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
E/AndroidRuntime( 2386):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
E/AndroidRuntime( 2386):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
E/AndroidRuntime( 2386):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
E/AndroidRuntime( 2386):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
E/AndroidRuntime( 2386):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
E/AndroidRuntime( 2386):    at android.app.Activity.performStart(Activity.java:5143)
E/AndroidRuntime( 2386):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
E/AndroidRuntime( 2386):    ... 11 more
W/ActivityManager(  276):   Force finishing activity se.appname.android/.activities.Main

2 个答案:

答案 0 :(得分:0)

尝试将此逻辑用于 onPostCreate() onStart()方法。

答案 1 :(得分:0)

我发现了问题,因为我使用PagerAdapter我需要等待回调finishUpdate(ViewGroup container)