我正在尝试实现映射,但遇到不可转换类型的错误;无法将 android.support.v4.app.Fragment
转换为 com.google.android.gms.maps.SupportMapFragment
我看过一些资源,但对我没有用
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'
最佳答案
根据 Google Play services Android release notes for play-services-maps:17.0.0
:
Warning: This release is a MAJOR version update and breaking change.
- Update your app to use Jetpack (AndroidX); follow the instructions in Migrating to AndroidX.
map 17.0.0
已切换到 AndroidX .这意味着 SupportMapFragment
现在扩展了 androidx.fragment.app.Fragment
,而不是等效的支持库。您需要切换回 16.1.0
或将您的应用迁移到 AndroidX。
关于java - 不可转换的类型无法转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963327/