我像 1 年前一样制作了这个应用程序,当我打开它时,它会弹出一堆完全随机的错误......可能是由于更新,但随后
error: incompatible types: Fragment cannot be converted to SupportMapFragment
.findFragmentById(R.id.map);
地图活动代码:
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;
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.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses =geocoder.getFromLocation(MainActivity.Globals.Latitude, MainActivity.Globals.Longitude,1);
if (listAddresses != null && listAddresses.size() > 0) {
String address = "";
if (listAddresses.get(0).getThoroughfare() != null) {
address += listAddresses.get(0).getThoroughfare() + " ";//Address
}
if (listAddresses.get(0).getLocality() != null) {
address += listAddresses.get(0).getLocality() + " "; //City
}
if (listAddresses.get(0).getAdminArea() != null) {
address += listAddresses.get(0).getAdminArea() + " ";//State
}
if (listAddresses.get(0).getCountryName() != null) {
address += listAddresses.get(0).getCountryName() + " ";//Country
}
if (listAddresses.get(0).getAdminArea() != null) {
address += listAddresses.get(0).getPostalCode();
}
Toast.makeText(MapsActivity.this, address, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
//Customization
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
UiSettings uiSettings = mMap.getUiSettings();
uiSettings.setCompassEnabled(true);
uiSettings.setZoomControlsEnabled(true);
LatLng person = new LatLng(MainActivity.Globals.Latitude, MainActivity.Globals.Longitude);
mMap.addMarker(new MarkerOptions().position(person).title("Person").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(person,12));
}
}
如果它有帮助 android 工作室让我改变一些 gradle 的东西
GradleProperties:
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
添加 Jetifiers 会给我一大堆错误,所以我没有这样做
Build.Gradle:
apply plugin: 'com.google.gms.google-services'
android {
signingConfigs {
config {
keyAlias 'Name'
keyPassword 'Password'
storeFile file('/Location')
storePassword 'Password'
}
}
compileSdkVersion 29
defaultConfig {
applicationId "com.example.appMaps"
minSdkVersion 23
targetSdkVersion 29
versionCode 7
versionName '7.0'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible,GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
//noinspection GradleCompatible
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.firebase:firebase-database:19.3.1'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.android.support:multidex:1.0.3'
//noinspection GradleCompatible,GradleCompatible
implementation 'com.android.support:design:26.1.0'
}
repositories {
mavenCentral()
}
最终有效的解决方案是使用 AppCompatActivity 而不是 FragmentActivity 然后还必须启用 Jetifier(谁能解释一下 Jetifier 是什么以及为什么使用它( 还必须在主代码中进行一些更改才能使用Snacker
从字面上看,必须将所有内容更改为androidX