gpt4 book ai didi

android - 有时 fragment 未附加到 Activity (onCreateOptionsMenu)

转载 作者:可可西里 更新时间:2023-11-01 19:08:27 25 4
gpt4 key购买 nike

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

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)

我已经修复并检查 fragment 是否附加到 Activity ,但这只是“避免”问题。在我看来,永远不要在 onCreateOptionsMenu 或 onOptionsItemSelected 中获得未附加状态。

为什么会这样? fragment 如何在不附加到 Activity 的情况下调用 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;
}

就像我说的,我现在检查 fragment 是否已附加,然后才调用 shouldUseMultipleColumns() 来解决问题,但没有解释为什么首先调用它...

Edit2:我的 Activity

    @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 的其他 fragment ,因此我的 NewsOverViewFragment 可能未附加。但即使是这种情况 - 为什么要调用 onCreateOptionsMenu?

最佳答案

你应该检查 fragment 的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;
}

关于android - 有时 fragment 未附加到 Activity (onCreateOptionsMenu),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com