android fragment中getActivity方法报错
PHPz
PHPz 2017-04-17 17:16:26
0
10
623

我想在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就是不行,请问应该怎么办?

PHPz
PHPz

学习是最好的投资!

reply all(10)
刘奇

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

PHPzhong

This. . . Come on

PHPzhong

。。。。。。。。

大家讲道理

onAttached() gets activity

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template