android – 有时Fragment不附加到Activity(onCreateOptionsMenu)

前端之家收集整理的这篇文章主要介绍了android – 有时Fragment不附加到Activity(onCreateOptionsMenu)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不知道为什么,但是我从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()来修复问题,但不能解释为什么这被称为首先…

编辑2:我的活动

@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?

解决方法

有时,使用onPostExecute方法时会出现此错误.
我这样处理了:
protected void onPostExecute(Document result) {
        if (!isAdded())
            return;
            ........
}
原文链接:https://www.f2er.com/android/311952.html

猜你在找的Android相关文章