【问题标题】:Android Maps app not finding the correct MapViewAndroid 地图应用找不到正确的 MapView
【发布时间】:2013-02-20 00:13:16
【问题描述】:

我制作了一个使用 Google Android Maps API V2 的应用,并且我按照 Google 提供的指南中的每一个步骤进行操作,但不幸的是它无法正常运行,并且每次尝试启动它时都会崩溃。

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

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

Manifest.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>

【问题讨论】:

  • com.cornboyzmaps.googlemapsA.MainActivity 是有问题的类。您正在显示com.example.name.MainActivity 的代码清单。您的应用中是否有一个名为 com.cornboyzmaps.googlemapsA 的单独包,其中可能包含您尝试运行的单独 MainActivity
  • 是的,真正的包名为 com.cornboyzmaps.googlemapsA(我也将其更改为 com.example.name.MainActivity,因为它也表明它与我的代码类似,结果太愚蠢了)这是我实际使用的那个(MainActivity.java 代码来自 com.cornboyzmaps.googlemapsA)。
  • 从您在此处发布的内容中看不出任何明显会导致此错误的内容。尝试创建一个单独的项目来重现错误。如果可以的话,将该项目完整地发布到某个地方。如果您无法使用单独的项目重现错误,那么您需要逐步使您的单独项目看起来更像您的主项目,直到错误重新出现或您的单独项目具有所有正确的代码(在这种情况下,只需将您的开发切换到单独的项目)。

标签: java android api maps


【解决方案1】:

在 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 错误但您的代码中没有错误也很奇怪。也许您可以发布更多代码?

【讨论】:

  • OP正在将片段添加到内置的android.R.id.contentFrameLayout,这是完全合理的。不过,这确实意味着布局 XML 文件是多余的。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2017-12-04
    • 2016-07-15
    • 2017-03-20
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多