Как настроить карту в моем приложении?

Я пытаюсь настроить карты в моем приложении, но я получаю фатальное исключение. Я получил ключ Google API и добавил его в файл манифеста.

Мой XML-файл:

<RelativeLayout 

android_id="@+id/activity_main"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
tools_context="com.example.aadesh.maps.MainActivity">

<com.google.android.gms.maps.MapFragment
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    android_layout_alignParentTop="true"
    android_layout_centerHorizontal="true"
    android_layout_marginTop="201dp"
    android_id="@+id/mapFragment" />

Были также некоторые проблемы визуализации в макете.

Основная деятельность:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    //add a marker and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));



}
}

Вот моя ошибка logcat:

FATAL EXCEPTION: main
                                                                   Process: com.example.aadesh.maps, PID: 6677
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aadesh.maps/com.example.aadesh.maps.MainActivity}: android.view.InflateException: Binary XML file line #13: Class is not a View com.google.android.gms.maps.MapFragment
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                       at android.app.ActivityThread.access0(ActivityThread.java:166)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:146)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5511)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                    Caused by: android.view.InflateException: Binary XML file line #13: Class is not a View com.google.android.gms.maps.MapFragment
                                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:617)
                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                       at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)
                                                                       at android.app.Activity.setContentView(Activity.java:2009)
                                                                       at com.example.aadesh.maps.MainActivity.onCreate(MainActivity.java:21)
                                                                       at android.app.Activity.performCreate(Activity.java:5322)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)&nbsp;
                                                                       at android.app.ActivityThread.access0(ActivityThread.java:166)&nbsp;
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)&nbsp;
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)&nbsp;
                                                                       at android.os.Looper.loop(Looper.java:146)&nbsp;
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5511)&nbsp;
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)&nbsp;
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)&nbsp;
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)&nbsp;
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)&nbsp;
                                                                       at dalvik.system.NativeStart.main(Native Method)&nbsp;
                                                                    Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.view.View
                                                                       at java.lang.Class.asSubclass(Class.java:1259)
                                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:565)
                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)&nbsp;
                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)&nbsp;
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:498)&nbsp;
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:398)&nbsp;
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:354)&nbsp;
                                                                       at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)&nbsp;
                                                                       at android.app.Activity.setContentView(Activity.java:2009)&nbsp;
                                                                       at com.example.aadesh.maps.MainActivity.onCreate(MainActivity.java:21)&nbsp;
                                                                       at android.app.Activity.performCreate(Activity.java:5322)&nbsp;
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)&nbsp;
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)&nbsp;
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)&nbsp;
                                                                       at android.app.ActivityThread.access0(ActivityThread.java:166)&nbsp;
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)&nbsp;
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)&nbsp;
                                                                       at android.os.Looper.loop(Looper.java:146)&nbsp;
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5511)&nbsp;
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)&nbsp;
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)&nbsp;
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)&nbsp;
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)&nbsp;
                                                                       at dalvik.system.NativeStart.main(Native Method)&nbsp;

В чем может заключаться проблема и как ее решить?

Вот файл gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
    applicationId "com.example.aadesh.maps"
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-maps:9.8.00'
}

1 ответ

  1. Пожалуйста, используйте фрагмент вместо класса MapFragment в XML-файле. Изменить как показано ниже код

    <fragment
            android_name="com.google.android.gms.maps.SupportMapFragment"
            android_layout_width="match_parent"
            android_layout_height="match_parent"
            android_layout_alignParentTop="true"
            android_layout_centerHorizontal="true"
            android_layout_marginTop="201dp"
            android_id="@+id/mapFragment" />