无法在Fragment中解析方法'getSupportFragmentManager()'

Irw*_*ans 43 android google-maps fragment android-fragments

我发现消息无法解析方法'getSupportFragmentManager()'我想要片段化为活动.因为我想在选项卡上使用地图滑动.

public class PETAcikarangsukatani extends Fragment {
Context context;

private static final LatLng SUKATANI = new LatLng(-6.171327, 107.178108);
private static final LatLng WRPOJOK = new LatLng(-6.222411, 107.162158);
private static final LatLng PILAR = new LatLng(-6.257033, 107.156472);
private static final LatLng CIKARANG = new LatLng(-6.256073, 107.143984);


GoogleMap googleMap;
final String TAG = "PathGoogleMapActivity";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_path_google_map, container, false);
    context = rootView.getContext();

    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map);
    googleMap = fm.getMap();
Run Code Online (Sandbox Code Playgroud)

Bla*_*elt 70

Fragment子类内部,您必须使用它getFragmentManager来代替getSupportFragmentManager.如果导入来自支持库,您将获得支持.


Md *_*han 45

对于我的情况,我确保从中导入Fragment

android.support.v4.app.Fragment

不是来自

android.app.Fragment

然后我用了

.getActivity()getSupportFragmentManager();

现在它的工作.如果我使用我在onAttach()中获得的活动实例也不起作用.


Vic*_*cky 33

扩展FragmentActivity而不是Activity


小智 13

如果您在api 21中工作,请使用 getFragmentManager()替换getSupportFragmentManager(). 或者 如果您的应用程序支持早于3.0的Android版本,请确保使用支持库设置Android项目,如设置项目中所述使用库并且这次使用getSupportFragmentManager().


Gab*_*nyu 12

使用 getActivity().getSupportFragmentManager()


Leo*_*der 6

您也可以使用AppCompatActivity而不是Activity.
然后getSupportFragmentManager()会工作.
在这种情况下,也不要忘记让您的App主题扩展AppCompat主题


Sid*_*ted 5

根据文档,getSupportFragmentManager()仅存在于AppCompatActivity类中.它不存在于Activity类中.因此,请确保您的类扩展了AppCompatActivity类.

public class MainActivity extends AppCompatActivity {
}
Run Code Online (Sandbox Code Playgroud)