大佬教程收集整理的这篇文章主要介绍了android – 有时Fragment不附加到Activity(onCreateOptionsMenu),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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?
以上是大佬教程为你收集整理的android – 有时Fragment不附加到Activity(onCreateOptionsMenu)全部内容,希望文章能够帮你解决android – 有时Fragment不附加到Activity(onCreateOptionsMenu)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。