我想在fragment中获取activity对象,但是无法使用getActivity方法。
activity中创建fragment对象
public void initFragment() {
// TODO Auto-generated method stub
FragmentManager fManager=getSupportFragmentManager();
FragmentTransaction transaction=fManager.beginTransaction();
transaction.replace(R.id.fl_content,new ContentFragment(), FRAGMENT_CONTENT); //是否为构造时要求的假的fragment容器??new对象的时候才是真正显示的布局
transaction.replace(R.id.fl_left_menu,new LeftMenuFragment(), FRAGMENT_LEFT_MENU);
transaction.commit();
fManager.findFragmentByTag(FRAGMENT_LEFT_MENU);
}
我想在fragment基类中获取activity就是不行,请问应该怎么办?
getActivity()
Wrong writing, there is an extra "u"I didn’t see where you used getActivity(). Your problem is not the same as your screenshot.
getactivity is spelled wrong
Even if you spell it correctly, there will be a NullPointerException problem when calling getActivity in the onCreate method. At this time, the fragment has not been attached. It is recommended that the subject take a look at the fragment life cycle
Hey...
Hahahaha, spelled wrong
getactivity was written incorrectly
This. . . Come on
。。。。。。。。
onAttached() gets activity