Cannot call getSupportFragmentManager() from activ

2019-01-14 14:26发布

I have an activity which has a fragment.

XML:

 <fragment android:name="com.example.androidcalculator.ResultFragment"
            android:id="@+id/result_fragment"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

And I want to call a method from ResultFragment from a method in the Activity, but getSupportFragmentManager "doesn't exist":

FragmentManager fragMan = getSupportFragmentManager();

How can I resolve this?

9条回答
闹够了就滚
2楼-- · 2019-01-14 15:05

extend class to AppCompatActivity instead of Activity

查看更多
闹够了就滚
3楼-- · 2019-01-14 15:05

extend FragementAvtivity instead of Activity

查看更多
Viruses.
4楼-- · 2019-01-14 15:17

Your activity doesn't extend FragmentActivity from the support library, therefore the method is not present in the superclass

If you are targeting api 11 or above, you could use Activity.getFragmentManager instead.

查看更多
贪生不怕死
5楼-- · 2019-01-14 15:18

i used FragmentActivity TabAdapter = new TabPagerAdapter(((FragmentActivity) getActivity()).getSupportFragmentManager());

查看更多
祖国的老花朵
6楼-- · 2019-01-14 15:19

import

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
查看更多
男人必须洒脱
7楼-- · 2019-01-14 15:21

get current activity from parent, then using this code

getActivity().getSupportFragmentManager()
查看更多
登录 后发表回答