- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
不知道为什么,但我从 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/
我以前制作过菜单,所以我不是一个完全的新手,但是我遇到了一个问题,Eclipse 告诉我必须删除 @Override,但显然如果我这样做,我的代码将不会运行到膨胀菜单。代码是: @Override p
我在 Android 应用程序中使用自己的样式,但它没有让我看到菜单(3 个点)和 Activity 标题。有什么建议吗? false true 但是在我的 MainActivit
菜单在应用程序的工具栏中可见,我可以打开它并查看菜单项,但是 onCreateOptionsMenu() 函数没有被调用,我通过设置断点知道这一点和调试,onOptionsItemSelected()
我是新手 Android 程序员,所以我想为我的应用程序制作菜单,但当我尝试将其启动到 AVD 时出现一些错误这是我的代码: import android.support.v7.app.AppComp
我的应用程序有两个 fragment ,一个是列表,一个是详细 View ,它们都以横向模式显示,只有列表以纵向显示。在配置更改时,如果我在两个 fragments 的 onCreateOptions
我的应用程序有这样的东西: TabActivity Tab 1 (ActivityGroup) Activity > Activity > MapActivity Ta
我正在使用 onCreateOptionsMenu 并且有一个我没有编程的默认菜单项。我想删除它,因为我对它没有用,它也没有做任何事情。这是它的样子: 我想去掉“设置”项。 @Override pub
由于我是 Android 编程的新手,我遇到了另一件我不明白的小事。为什么下面的 onCreateOptionsMenu 方法返回 super.onCreateOptionsMenu 而不是仅仅调用
我有一个简单的应用程序。 这里是MyActivity.java public class MyActivity extends Activity { /** * Called whe
我想从代码中调用 onCreateOptionsMenu 因为如果我使用: menu.setGroupEnabled(0, false); 与: public Menu menu; public bo
这是我创建的一个简单的 android 应用程序来演示我的问题: public class OptionMenuTest extends Activity { @Override pr
如何在我的代码中触发 Android onCreateOptionsMenu 函数,即无需用户单击手机上的选项菜单按钮? 最佳答案 Activity.openOptionsMenu(); 就可以了 关
我在这篇文章中发现了一些令我困惑的地方 http://developer.android.com/guide/topics/ui/menus.html 下面是一段简单的代码和输出当我在 android
我遇到的问题是,当我点击菜单按钮时,我的应用程序没有显示选项菜单。调试显示点击菜单按钮后未调用 onCreateOptionsMenu(Menu menu) 方法。我有另一个具有相同菜单代码的应用程序
我完全不知道为什么会这样。选项菜单工作正常,但现在不再工作了。 当我点击菜单按钮时,菜单打开,我点击它但什么也没有。当我执行 LogCat 时,它确实注册了 MenuItem ...我真的要拉扯我的头
我在获取在 Android 中运行的选项菜单时遇到了一些问题。我之前构建了应用程序,它们都运行良好,但现在菜单不弹出。 代码: @Override public boolean onCreateOpt
我有一个通过以下方式创建的菜单: @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("Email")
我想知道在 Android 中是否有任何方式以编程方式知道是否 公共(public) bool onCreateOptionsMenu(菜单菜单)已调用并返回。 我在一个项目中使用 actionshe
在我的 android Activity 中,我使用这个主题删除了操作栏 但我的问题是“onCreateOptionsMenu”没有显示,我试过这个方法来创建请看 private void ge
我想在每次调用时刷新选项菜单, 我有一个功能,当用户在选项菜单中单击选项(添加/删除收藏夹)时,它会检查它是否是收藏夹并执行该功能, 问题:创建菜单后,它不会在用户第二次按下选项按钮时刷新 onCre
我是一名优秀的程序员,十分优秀!