2017-08-24 9 views
0

アプリケーションを正常に実行した場合、if文にブレークポイントを設定すると、setUserVisibleHint()にブレークポイントが設定され、例外: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

同じ問題:( –

関連する問題