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)
Md *_*han 45
对于我的情况,我确保从中导入Fragment类
android.support.v4.app.Fragment
不是来自
android.app.Fragment
然后我用了
.getActivity()getSupportFragmentManager();
现在它的工作.如果我使用我在onAttach()中获得的活动实例也不起作用.
小智 13
如果您在api 21中工作,请使用 getFragmentManager()替换getSupportFragmentManager(). 或者 如果您的应用程序支持早于3.0的Android版本,请确保使用支持库设置Android项目,如设置项目中所述使用库并且这次使用getSupportFragmentManager().
您也可以使用AppCompatActivity
而不是Activity.
然后getSupportFragmentManager()
会工作.
在这种情况下,也不要忘记让您的App主题扩展AppCompat主题
根据文档,getSupportFragmentManager()仅存在于AppCompatActivity类中.它不存在于Activity类中.因此,请确保您的类扩展了AppCompatActivity类.
public class MainActivity extends AppCompatActivity {
}
Run Code Online (Sandbox Code Playgroud)