在setUserVisibleHint()

时间:2017-08-24 13:05:36

标签: android android-fragments

如果我正常运行应用程序它会正常工作,当我在setUserVisibleHint()中的if语句上放置一个断点时,它会因异常而崩溃:java.lang.IllegalStateException: No activity

那会是什么问题?

public class MyFragment extends Fragment 


public MyFragment() {
    // Required empty public constructor
}

public static CommunityFragment newInstance() {

    Bundle args = new Bundle();

    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment     
    return inflater.inflate(R.layout.fragment_community, container, false);
}


@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (getActivity() != null && isVisibleToUser) {
       // fragment is visible
        }
    } else if (getActivity() != null) {
       // fragment is invisible
        }
    }
  } 
}

logcat的:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          PID: 8669
                                                                         java.lang.IllegalStateException: No activity
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1578)
                                                                             at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2883)
                                                                             at android.support.v4.app.Fragment.onCreate(Fragment.java:1268)
                                                                             at android.support.v4.app.Fragment.performCreate(Fragment.java:2177)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1244)
                                                                             at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1080)
                                                                             at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:971)
                                                                             at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
                                                                             at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143)
                                                                             at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                                                                             at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1979)
                                                                             at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                                                                             at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                             at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                                                                             at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                             at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                             at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                             at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                                             at android.view.View.measure(View.java:18788)
                                                                             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                                             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                            at android

1 个答案:

答案 0 :(得分:2)

而不是

if (getActivity() != null && isVisibleToUser) {
       // fragment is visible
        }
    } else if (getActivity() != null) {
       // fragment is invisible
        }

尝试

if (getView() != null) {
       // fragment is visible
        }

它对我有用。