【发布时间】:2016-09-16 17:13:56
【问题描述】:
我在我的项目中使用了滑动菜单,所有的侧边菜单项都是片段。
我的主页片段也有两个标签页。 (片段 A 有两个子片段作为选项卡;片段 B 和 C)。每当我从其他片段返回到 homefragment 时,fragmentA 的 OnResume() 就会被调用。如何调用 FragmentA 的子 FragmentB 的 onResume() ?
我想在加载 homefragment 时刷新 FragmentB 中的视图,我认为唯一的选择是在 OnResume() 中执行。
我尝试在 homefragment(即 fragmentA)的onResume() 中调用refreshFragment() 方法,但它抛出了一个错误,指出片段未附加到活动。
FragemntA的onResume()代码
@Override
public void onResume() {
super.onResume();
mTabHost.setCurrentTab(mTabHost.getCurrentTab());
boolean settingschanged = sharedPrefrences.getBoolean(Constants.CHECK_CHANGE, false);
int unit = sharedPrefrences.getInt(Constants.DIMENSION_UNIT, 0);
SharedPreferences.Editor editor = sharedPrefrences.edit();
switch (mTabHost.getCurrentTab()) {
case 0:
((Outer) mFragments.get(0)).refreshView(unit);
((Outer) mFragments.get(0)).refreshFragment(settingschanged);
editor.putInt(Constants.DIMENSION_UNIT, unit);
editor.commit();
break;
case 1:
((Inner) mFragments.get(1)).refreshView(unit);
editor.putInt(Constants.DIMENSION_UNIT, unit);
editor.commit();
break;
default:
((Outer) mFragments.get(0)).refreshView(unit);
editor.putInt(Constants.DIMENSION_UNIT, unit);
editor.commit();
}
}
下面是我遇到的崩溃:
09-16 14:13:34.608: E/AndroidRuntime(5758): java.lang.IllegalStateException: Fragment Outer{d34ce61} not attached to Activity
09-16 14:13:34.608: E/AndroidRuntime(5758): at android.support.v4.app.Fragment.getResources(Fragment.java:639)
【问题讨论】:
-
在onResume中不要刷新还有很多其他方法可以刷新
-
@siddhesh 你能详细说明我可以使用哪些其他方法
-
在片段a的onResume()中,请检查当前正在运行的子片段,并知道通过使用片段A中的对象调用子片段所需的函数。
标签: android android-fragments illegalstateexception onresume