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

时间:2014-08-30 13:13:30

标签: 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)

我已经做了一个修复,并检查片段是否附加到活动,但这只是"避免"问题。在我看来,在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;
}

就像我说的那样,我现在检查片段是否已附加,然后调用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();
        }
    }
    ...
}

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

1 个答案:

答案 0 :(得分:0)

您应该检查片段的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;
}