0

我制作了一个使用 Google Android Maps API V2 的应用程序,并且我遵循了 Google 提供的指南中的每一步,但不幸的是它不能正常工作,每次我尝试启动它时它都会崩溃。

问题似乎在于它不是在寻找 com.google.android.gms.maps.MapView 这是 Android Maps API V2 的 MapView 而是在寻找 com.google.android.maps.MapView 这是 MapView对于 Android Maps API V1,我想知道如何更改它?

03-04 00:26:51.274: E/dalvikvm(14369): Could not find class 'com.google.android.maps.MapView', referenced from method com.cornboyzmaps.googlemapsA.MainActivity.onCreate

MainActivity.java

package com.example.name;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SupportMapFragment fragment = new SupportMapFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, fragment).commit();
    }
}

activity_main.xml

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

清单.xml

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

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

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

    <uses-permission android:name="com.example.name.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:debuggable="true" >
        <activity
            android:name="com.example.name.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="**api_key**" />
    </application>

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

</manifest>
4

2 回答 2

0

您是否尝试过使用 setContentView 正常扩展 xml 布局?现在看起来您正在尝试从 xml 片段制作片段。

于 2013-03-05T14:45:12.340 回答
0

在activity_main.xml 中,您不应该有地图片段,因为您以编程方式添加它。
我要做的是将activity_mail.xml 更改为包含将成为片段容器的Framelayout。例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

之后在 MainActivity.java 中,您应该为 Framelayout 找到 ViewbyId 并添加 mapfragment。
同样奇怪的是,您收到 MapView 错误但您的代码中没有错误。也许您可以发布更多代码?

于 2013-03-05T14:48:13.863 回答