2017-08-24 180 views
0

如果我通常運行應用程序,它會工作得很好,當我在setUserVisibleHint()的if語句中放置斷點時,它會崩潰,並出現異常:java.lang.IllegalStateException: No activity當在調試時啓動setUserVisibleHint()時,應用程序崩潰

那麼會是什麼問題呢?

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

你可以發佈整個logcat嗎? – azizbekian

+1

父類似乎不會拋出任何異常 https://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean) public void setUserVisibleHint(boolean isVisibleToUser) – Killer

+0

@azizbekian我做了:) –

回答

1

而不是

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

嘗試

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

它爲我工作。

+0

同樣的問題:( –