@@ -428,92 +428,26 @@ public void run() {
428428 }
429429 };
430430
431- private void logViewHierarchy (String prefix , View view ) {
432- StringBuilder builder = new StringBuilder (128 );
433- builder .append (prefix );
434- DebugUtils .buildShortClassTag (view , builder );
435- int id = view .getId ();
436- if (id != -1 ) {
437- builder .append (" #" );
438- builder .append (Integer .toHexString (id ));
439- if (id != 0 && id != -1 ) {
440- try {
441- String pkgname ;
442- switch (id &0xff000000 ) {
443- case 0x7f000000 :
444- pkgname ="app" ;
445- break ;
446- case 0x01000000 :
447- pkgname ="android" ;
448- break ;
449- default :
450- pkgname = view .getResources ().getResourcePackageName (id );
451- break ;
452- }
453- String typename = view .getResources ().getResourceTypeName (id );
454- String entryname = view .getResources ().getResourceEntryName (id );
455- builder .append (" (" );
456- builder .append (pkgname );
457- builder .append (":" );
458- builder .append (typename );
459- builder .append ("/" );
460- builder .append (entryname );
461- builder .append (")" );
462- } catch (Resources .NotFoundException e ) {
463- }
464- }
465- }
466- Object tag = view .getTag ();
467- if (tag != null ) {
468- builder .append (" " );
469- builder .append (tag );
470- }
471- builder .append ("}" );
472- Log .e (TAG , builder .toString ());
473-
474- if (!(view instanceof ViewGroup )) {
475- return ;
476- }
477- ViewGroup grp = (ViewGroup )view ;
478- final int N = grp .getChildCount ();
479- if (N <= 0 ) {
480- return ;
481- }
482- prefix = prefix + " " ;
483- for (int i =0 ; i <N ; i ++) {
484- logViewHierarchy (prefix , grp .getChildAt (i ));
485- }
486- }
487-
488- private void throwNoViewFound (Fragment f ) {
489- String msg = "No view found for id 0x"
490- + Integer .toHexString (f .mContainerId ) + " ("
491- + f .getResources ().getResourceName (f .mContainerId )
492- + ") for fragment " + f ;
493- Log .e (TAG , msg );
494- Log .e (TAG , "Activity state:" );
495- if (f .getActivity () != null ) {
431+ private void throwException (RuntimeException ex ) {
432+ Log .e (TAG , ex .getMessage ());
433+ LogWriter logw = new LogWriter (Log .ERROR , TAG );
434+ PrintWriter pw = new PrintWriter (logw );
435+ if (mActivity != null ) {
436+ Log .e (TAG , "Activity state:" );
496437 try {
497- LogWriter logw = new LogWriter (Log .ERROR , TAG );
498- PrintWriter pw = new PrintWriter (logw );
499- f .getActivity ().dump (" " , null , pw , new String [] { });
438+ mActivity .dump (" " , null , pw , new String [] { });
500439 } catch (Exception e ) {
501440 Log .e (TAG , "Failed dumping state" , e );
502441 }
503442 } else {
504- Log .e (TAG , " NULL ACTIVITY!" );
505- }
506- Log .e (TAG , "View hierarchy:" );
507- if (f .getActivity () != null ) {
443+ Log .e (TAG , "Fragment manager state:" );
508444 try {
509- logViewHierarchy (" " , f . getActivity (). getWindow (). getDecorView () );
445+ dump (" " , null , pw , new String [] { } );
510446 } catch (Exception e ) {
511- Log .e (TAG , "Failed dumping view hierarchy " , e );
447+ Log .e (TAG , "Failed dumping state " , e );
512448 }
513- } else {
514- Log .e (TAG , " NULL ACTIVITY!" );
515449 }
516- throw new IllegalArgumentException ( msg ) ;
450+ throw ex ;
517451 }
518452
519453 @ Override
@@ -608,8 +542,8 @@ public void removeOnBackStackChangedListener(OnBackStackChangedListener listener
608542 @ Override
609543 public void putFragment (Bundle bundle , String key , Fragment fragment ) {
610544 if (fragment .mIndex < 0 ) {
611- throw new IllegalStateException ("Fragment " + fragment
612- + " is not currently in the FragmentManager" );
545+ throwException ( new IllegalStateException ("Fragment " + fragment
546+ + " is not currently in the FragmentManager" )) ;
613547 }
614548 bundle .putInt (key , fragment .mIndex );
615549 }
@@ -621,22 +555,22 @@ public Fragment getFragment(Bundle bundle, String key) {
621555 return null ;
622556 }
623557 if (index >= mActive .size ()) {
624- throw new IllegalStateException ("Fragement no longer exists for key "
625- + key + ": index " + index );
558+ throwException ( new IllegalStateException ("Fragement no longer exists for key "
559+ + key + ": index " + index )) ;
626560 }
627561 Fragment f = mActive .get (index );
628562 if (f == null ) {
629- throw new IllegalStateException ("Fragement no longer exists for key "
630- + key + ": index " + index );
563+ throwException ( new IllegalStateException ("Fragement no longer exists for key "
564+ + key + ": index " + index )) ;
631565 }
632566 return f ;
633567 }
634568
635569 @ Override
636570 public Fragment .SavedState saveFragmentInstanceState (Fragment fragment ) {
637571 if (fragment .mIndex < 0 ) {
638- throw new IllegalStateException ("Fragment " + fragment
639- + " is not currently in the FragmentManager" );
572+ throwException ( new IllegalStateException ("Fragment " + fragment
573+ + " is not currently in the FragmentManager" )) ;
640574 }
641575 if (fragment .mState > Fragment .INITIALIZING ) {
642576 Bundle result = saveFragmentBasicState (fragment );
@@ -913,7 +847,11 @@ void moveToState(Fragment f, int newState, int transit, int transitionStyle,
913847 if (f .mContainerId != 0 ) {
914848 container = (ViewGroup )mActivity .findViewById (f .mContainerId );
915849 if (container == null && !f .mRestored ) {
916- throwNoViewFound (f );
850+ throwException (new IllegalArgumentException (
851+ "No view found for id 0x"
852+ + Integer .toHexString (f .mContainerId ) + " ("
853+ + f .getResources ().getResourceName (f .mContainerId )
854+ + ") for fragment " + f ));
917855 }
918856 }
919857 f .mContainer = container ;
@@ -1674,12 +1612,9 @@ Parcelable saveAllState() {
16741612 Fragment f = mActive .get (i );
16751613 if (f != null ) {
16761614 if (f .mIndex < 0 ) {
1677- String msg = "Failure saving state: active " + f
1678- + " has cleared index: " + f .mIndex ;
1679- Slog .e (TAG , msg );
1680- dump (" " , null , new PrintWriter (new LogWriter (
1681- Log .ERROR , TAG , Log .LOG_ID_SYSTEM )), new String [] { });
1682- throw new IllegalStateException (msg );
1615+ throwException (new IllegalStateException (
1616+ "Failure saving state: active " + f
1617+ + " has cleared index: " + f .mIndex ));
16831618 }
16841619
16851620 haveFragments = true ;
@@ -1692,12 +1627,9 @@ Parcelable saveAllState() {
16921627
16931628 if (f .mTarget != null ) {
16941629 if (f .mTarget .mIndex < 0 ) {
1695- String msg = "Failure saving state: " + f
1696- + " has target not in fragment manager: " + f .mTarget ;
1697- Slog .e (TAG , msg );
1698- dump (" " , null , new PrintWriter (new LogWriter (
1699- Log .ERROR , TAG , Log .LOG_ID_SYSTEM )), new String [] { });
1700- throw new IllegalStateException (msg );
1630+ throwException (new IllegalStateException (
1631+ "Failure saving state: " + f
1632+ + " has target not in fragment manager: " + f .mTarget ));
17011633 }
17021634 if (fs .mSavedFragmentState == null ) {
17031635 fs .mSavedFragmentState = new Bundle ();
@@ -1736,12 +1668,9 @@ Parcelable saveAllState() {
17361668 for (int i =0 ; i <N ; i ++) {
17371669 added [i ] = mAdded .get (i ).mIndex ;
17381670 if (added [i ] < 0 ) {
1739- String msg = "Failure saving state: active " + mAdded .get (i )
1740- + " has cleared index: " + added [i ];
1741- Slog .e (TAG , msg );
1742- dump (" " , null , new PrintWriter (new LogWriter (
1743- Log .ERROR , TAG , Log .LOG_ID_SYSTEM )), new String [] { });
1744- throw new IllegalStateException (msg );
1671+ throwException (new IllegalStateException (
1672+ "Failure saving state: active " + mAdded .get (i )
1673+ + " has cleared index: " + added [i ]));
17451674 }
17461675 if (DEBUG ) Log .v (TAG , "saveAllState: adding fragment #" + i
17471676 + ": " + mAdded .get (i ));
@@ -1846,8 +1775,8 @@ void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
18461775 for (int i =0 ; i <fms .mAdded .length ; i ++) {
18471776 Fragment f = mActive .get (fms .mAdded [i ]);
18481777 if (f == null ) {
1849- throw new IllegalStateException (
1850- "No instantiated fragment for index #" + fms .mAdded [i ]);
1778+ throwException ( new IllegalStateException (
1779+ "No instantiated fragment for index #" + fms .mAdded [i ])) ;
18511780 }
18521781 f .mAdded = true ;
18531782 if (DEBUG ) Log .v (TAG , "restoreAllState: making added #" + i + ": " + f );
@@ -1875,7 +1804,7 @@ void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
18751804 }
18761805
18771806 public void attachActivity (Activity activity ) {
1878- if (mActivity != null ) throw new IllegalStateException ();
1807+ if (mActivity != null ) throw new IllegalStateException ("Already attached" );
18791808 mActivity = activity ;
18801809 }
18811810
0 commit comments