0

我正在尝试在我的 samsung s3 mini (4.1.2) 上使用 google android maps V2,但是我不断收到消息“应用程序已意外停止”。我已经包含了谷歌 API [Android 4.1.2]、谷歌播放服务库、android-support-v4.jar。以下是代码: 在我的 AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Test.projecttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.Test.projecttest.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.Test.projecttest.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBjT4hZc4gKo1lyrQ5fwLD_Fz5vWgUQmlA" />

        <activity
            android:name="com.Test.projecttest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

com.Test.projecttest 中的 MainActivity.java

      package com.Test.projecttest;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

main.xml 位于 res/layout

    <?xml version="1.0" encoding="utf-8"?>
<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

我的日志猫:

07-19 16:21:46.594: E/AndroidRuntime(5069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
07-19 16:21:46.594: E/AndroidRuntime(5069): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
4

3 回答 3

1

你的minsdk是8

 <uses-sdk
    android:minSdkVersion="8"

使用SupportFragment.

在您的布局 xml 中使用以下内容

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>

您的活动必须扩展FragmentActivity

还要将此添加到清单

    <permission
    android:name="com.Test.projecttest.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
    <uses-permission android:name="com.Test.projecttest.permission.MAPS_RECEIVE"/>

初始化地图对象

 SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
 GoogleMap mMap = fm.getMap(); 

确保您已添加支持库

还要确保您导入了以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment; 
于 2013-07-19T07:55:36.233 回答
0

您需要在清单中添加此内容:+ @Raghunandan 所说的所有内容;)

 <permission
    android:name="your.package.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="your.package.permission.MAPS_RECEIVE" />
于 2013-07-19T08:01:50.793 回答
0

不需要在xml中写。

只需将活动扩展到 SupportMapFragment

那么您将能够使用所有谷歌地图功能

并将其添加到清单中:

<permission     
android:name="com.Test.projecttest.permission.MAPS_RECEIVE" 
android:protectionLevel="signature"/>

我在片段上显示了地图并将片段附加到MainActivity. 它使它更加灵活。我是通过以下方式做到的:

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends SupportMapFragment implements
        LocationListener, OnMapClickListener, OnCameraChangeListener {
于 2013-07-19T08:01:59.750 回答