有时Fragment没有附加到Activity(onCreateOptionsMenu)

Fra*_*e91 14 android android-fragments

不知道为什么,但我从playstore中的应用程序收到一些错误报告,其中包含以下消息:

java.lang.IllegalStateException: Fragment NewsOverViewFragment{4062e840} not attached to Activity
   at android.support.v4.app.Fragment.getResources(Fragment.java:601)
   at de.dala.simplenews.ui.NewsOverViewFragment.shouldUseMultipleColumns(NewsOverViewFragment.java:153)
   at de.dala.simplenews.ui.NewsOverViewFragment.updateMenu(NewsOverViewFragment.java:145)
   at de.dala.simplenews.ui.NewsOverViewFragment.onCreateOptionsMenu(NewsOverViewFragment.java:139)
Run Code Online (Sandbox Code Playgroud)

我已经做了一个修复并检查片段是否附加到活动,但这只是"避免"问题.在我看来,在onCreateOptionsMenu或onOptionsItemSelected中获取未附加状态应该永远不会发生.

为什么会发生这种情况?片段如何在不附加到活动的情况下调用onCreateOptionsMenu/onOptionsItemSelected?

问候

码:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.news_overview_menu, menu);
    updateMenu();
    super.onCreateOptionsMenu(menu, inflater);
}

private void updateMenu(){
    boolean useMultiple = shouldUseMultipleColumns();
    ...
}

private boolean shouldUseMultipleColumns(){
    boolean useMultiple = false;

    Configuration config = getActivity().getResources().getConfiguration();
            switch (config.orientation) {
                case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
                    useMultiple = PrefUtilities.getInstance().useMultipleColumnsLandscape();
                    break;
                case android.content.res.Configuration.ORIENTATION_PORTRAIT:
                    useMultiple = PrefUtilities.getInstance().useMultipleColumnsPortrait();
                    break;
      }

    return useMultiple;
}
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,我现在检查片段是否附加,然后调用shouldUseMultipleColumns()哪个修复了问题,但没有解释为什么首先调用它...

Edit2:我的活动

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    if (savedInstanceState == null) {
        if (getIntent().getDataString() != null) {
            String path = getIntent().getDataString();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            currentFragment = CategoryModifierFragment.getInstance(path);
            transaction.replace(R.id.container, currentFragment).commit();
        } else {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            currentFragment = NewsOverViewFragment.getInstance(NewsOverViewFragment.ALL);
            transaction.replace(R.id.container, currentFragment).commit();
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

这基本上是附加程序.但是,可以调用其他始终替换的片段,因此可能无法currentFragment附加我的NewsOverViewFragment.但即使是这种情况 - 为什么onCreateOptionsMenu被调用?

gme*_*tax 1

你应该检查isAdded()片段的功能 http://developer.android.com/reference/android/app/Fragment.html#isAdded()

所以你的shouldUseMultipleColumns()意志看起来像那样

private boolean shouldUseMultipleColumns(){
    if(!isAdded()) return false;
    boolean useMultiple = false;

    Configuration config = getActivity().getResources().getConfiguration();
            switch (config.orientation) {
                case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
                    useMultiple = PrefUtilities.getInstance().useMultipleColumnsLandscape();
                    break;
                case android.content.res.Configuration.ORIENTATION_PORTRAIT:
                    useMultiple = PrefUtilities.getInstance().useMultipleColumnsPortrait();
                    break;
      }

    return useMultiple;
}
Run Code Online (Sandbox Code Playgroud)