Fragment填坑

坑A

 No view found for id 0x1020368 (android:id/webviewchromium_color_picker_text) for fragment DeviceConnectFragment{654df8c8 #0 id=0x1020368 com.***.***.***.fragment.DeviceConnectFragment}

如果你看到了如下error,但是这个error有很多种引起的原因,大部分是因为Fragment嵌套。我碰到的原因是:

  • Fragment没用进行嵌套
  • 使用的是PreferenceActivity和PreferenceFragment配合出现的error
  • 使用系统布局替换新Fragment(不是自己写的layout)
05-02 14:26:01.141 2909-2909/? E/FragmentManager: No view found for id 0x1020368 (android:id/webviewchromium_color_picker_text) for fragment DeviceConnectFragment{654df8c8 #0 id=0x1020368 com.***.***.***.***.DeviceConnectFragment}
    Activity state:
      Local Activity 65370df8 State:
        mResumed=true mStopped=false mFinished=false
        mLoadersStarted=true
        mChangingConfigurations=false
        mCurrentConfig={1.0 ?mcc?mnc zh_CN ldltr sw768dp w1024dp h768dp 160dpi xlrg land finger -keyb/v/h dpad/v s.4}
      Active Fragments in 65305b48:
05-02 14:26:01.151 2909-2909/? E/FragmentManager:     #0: DeviceConnectFragment{654df8c8 #0 id=0x1020368 com.***.***.***.fragment.DeviceConnectFragment}
          mFragmentId=#1020368 mContainerId=#1020368 mTag=com.***.***.***.fragment.DeviceConnectFragment
          mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
          mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
          mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
          mRetainInstance=false mRetaining=false mUserVisibleHint=true
          mFragmentManager=FragmentManager{65305b48 in SettingActivity{65370df8}}
          mActivity=com.***.***.***.SettingActivity@65370df8
          mNextAnim=17498112
        #1: AdvancedSettingFragment{655265e8 #1 id=0x10202f6}

    mFragmentId=#10202f6 mContainerId=#10202f6 mTag=null
          mState=5 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0
          mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
          mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
          mRetainInstance=false mRetaining=false mUserVisibleHint=true
          mFragmentManager=FragmentManager{65305b48 in SettingActivity{65370df8}}
          mActivity=com.***.***.***.SettingActivity@65370df8
          mContainer=android.preference.PreferenceFrameLayout{6539dff0 V.E..... .......D 0,0-632,768 #10202f6 android:id/prefs}
          mView=android.widget.LinearLayout{6553f228 V.E..... .......D 0,0-632,768}
      Added Fragments:
        #0: AdvancedSettingFragment{655265e8 #1 id=0x10202f6}
        #1: DeviceConnectFragment{654df8c8 #0 id=0x1020368 com.***.***.***.fragment.DeviceConnectFragment}
      Back Stack Indices:
        #0: BackStackEntry{654df870 #0 :android:prefs}
      FragmentManager misc state:
        mActivity=
05-02 14:26:01.161 2909-2909/? E/FragmentManager: com.***.***.***.SettingActivity@65370df8
        mContainer=android.app.Activity$1@652d56e8
        mCurState=5 mStateSaved=false mDestroyed=false
      ViewRoot:
        mAdded=true mRemoved=false
        mConsumeBatchedInputScheduled=true
        mPendingInputEventCount=0
        mProcessInputEventsScheduled=false
        mTraversalScheduled=true (barrier=519)
        android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
        android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
        android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
      Choreographer:
        mFrameScheduled=true
        mLastFrameTime=3335116 (28 ms ago)
      View Hierarchy:
        com.android.internal.policy.impl.PhoneWindow$DecorView{651376b8 V.E..... R......D 0,0-1024,768}
          android.widget.LinearLayout{65138478 V.E..... .......D 0,0-1024,768}
            android.view.ViewStub{651393f8 G.E..... ......I. 0,0-0,0 #102030e}
            android.widget.FrameLayout{65139ef0 V.E..... .......D 0,0-1024,768 #1020002 android:id/content}

    android.view.View{6513ab08 G.ED.... ......ID 0,0-0,0 #7f0f0334 app:id/media_frame_pager_cach_background}
05-02 14:26:01.171 2909-2909/? E/FragmentManager:           com.***.***.***.MediaPager{652219c0 I.E..... ......I. 0,0-1024,768 #7f0f0335 app:id/media_frame_media_pager}
                android.view.View{6512f620 V.ED.... ........ 0,0-1024,768 #7f0f0343 app:id/media_frame_pager_background}
                com.***.***.widget.CatchErrorImageView{6513bea0 I.ED.... ......I. 0,0-1024,768 #7f0f0344 app:id/media_frame_pager_cache_iv_ground}
                com.***.***.widget.photoview.HackyViewPager{650feb78 VFED.... ........ 0,0-1024,768 #7f0f0345 app:id/media_frame_pager_viewpager}
                  android.widget.RelativeLayout{65351fb0 V.E..... ........ 0,0-1024,768 #7f0f0364 app:id/media_layout_custom_ly}
                    android.widget.RelativeLayout{653529f0 V.E..... ........ 0,0-1024,768 #7f0f0365 app:id/media_layout_custom_photo}

    com.***.***.widget.photoview.PhotoView{65354ce8 V.ED.... ........ 0,0-1024,768 #7f0f036f app:id/media_view_photo_iv}
                      android.widget.ProgressBar{6539a728 G.ED.... ......I. 488,360-536,408 #7f0f0370 app:id/media_view_photo_progress}
                      android.widget.ImageView{6539dc58 G.ED..C. ......I. 0,0-0,0 #7f0f0371 app:id/left_gradient}
                      android.widget.ImageView{6539eb50 G.ED..C. ......I. 0,0-0,0 #7f0f0372 app:id/right_gradient}
                      android.widget.ImageView{6539f9d8 G.ED.... ......I. 0,0-0,0 #7f0f0373 app:id/media_view_photo_scale}
                    android.widget.RelativeLayout{653535a0 G.E..... ......I. 0,0-0,0 #7f0f0366 app:id/media_layout_custom_video}
                      com.***.***.widget.photoview.PhotoView{653a6c78 V.ED.... ......I. 0,0-0,0 #7f0f0376 app:id/media_view_video_iv}
                      android.widget.RelativeLayout{653a81d0 V.E..... ......I. 0,0-0,0 #7f0f0377 app:id/media_view_video_layout}

    android.widget.ProgressBar{653a8dc8 V.ED.... ......I. 0,0-0,0 #7f0f0378 app:id/media_view_video_progress}
                      android.widget.ImageView{653a9e98 V.ED.... ......I. 0,0-0,0 #7f0f0371 app:id/left_gradient}
                      android.widget.ImageView{653ac710 V.ED.... ......I. 0,0-0,0 #7f0f0372 app:id/right_gradient}
                      android.widget.ImageView{653ad3c0 G.ED..C. ......I. 0,0-0,0 #7f0f0379 app:id/media_view_video_control}
                      android.widget.RelativeLayout{653ae190 G.E..... ......I. 0,0-0,0 #7f0f037a app:id/video_progress_info}
                        android.widget.SeekBar{653b53d0 VF.D.... ......ID 0,0-0,0 #7f0f037b app:id/video_progress}
                        android.widget.TextView{653c4490 V.ED.... ......ID 0,0-0,0 #7f0f037c app:id/video_time_info}
                    android.widget.ImageView{653c9100 G.ED.... ......I. 0,0-0,0 #7f0f0367 app:id/media_layout_custom_newflag_iv}

    android.widget.ImageView{65086eb8 I.ED.... ......I. 0,0-1024,768 #7f0f0346 app:id/media_frame_pager_cache_iv}
                android.widget.RelativeLayout{65087b38 I.E...C. ......ID 0,618-1024,768 #7f0f0350 app:id/bottom_bar}
                  android.view.View{65222d80 I.ED.... ......I. 884,20-954,90 #7f0f0351 app:id/stub}
                  android.widget.ViewFlipper{65223fb8 V.E..... ......ID 0,0-1024,150 #7f0f0352 app:id/media_frame_pager_user_flipper}
                    android.widget.RelativeLayout{65225308 I.E..... ......I. 0,0-1024,150 #7f0f0356 app:id/media_frame_pager_temp_user_layout}
                      android.widget.RelativeLayout{65098ef0 G.E..... ......I. 0,0-0,0 #7f0f0357 app:id/rl_like}
                        android.widget.RelativeLayout{6509a130 V.E..... ......I. 0,0-0,0 #7f0f0358 app:id/interaction_layout}
                          android.widget.ImageView{6509b3d8 V.ED.... ......I. 0,0-0,0 #7f0f017a app:id/iv_like}

出错的是这句

transaction.replace(com.android.internal.R.id.prefs, new DeviceConnectFragment(), FRAGMENT_NAME);

当使用系统布局的时候,在低版本上面,会出现上面的问题。

解决方案

int identifier = Resources.getSystem().getIdentifier("prefs", "id", "android");
transaction.replace(identifier, new DeviceConnectFragment(), FRAGMENT_NAME);

就解决了

微信公众号
Fragment填坑_第1张图片

QQ群:365473065

你可能感兴趣的:(Fragment)