如何在我的应用程序中配置地图?

时间:2016-12-31 10:34:52

标签: android google-maps

我正在尝试在我的应用中配置地图,但我遇到了致命的异常。我已获取google API密钥并将其添加到清单文件中。

我的XML文件布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.access$900(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) 
                                                                       at android.app.ActivityThread.access$900(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: 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) 
                                                                       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) 
                                                                       at android.app.ActivityThread.access$900(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) 

可能是什么问题,我该如何解决?

这是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 个答案:

答案 0 :(得分:0)

请在XML文件中使用fragment而不是MapFragment Class。更改如下代码

<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" />