1

对不起,我发布了这个问题。我参考了很多链接。但仍然无法解决问题

  1. 我已经链接了 google-play-services_lib 服务。
  2. 将属性设置为 Google Inc.
  3. 从谷歌控制台获取 API_KEY。

这是我的 Manifest.xml:

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

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

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

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

     <uses-library android:name="com.google.android.maps"/>

    <activity
        android:name="com.abc.xyz.MapViews"
        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="removed" />

</application>

</manifest>

这是我的 .java 文件:

public class MapViews extends Activity {

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

这是我的 map_views.xml 文件:

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

添加我收到错误:

java.lang.RuntimeException: Unable to start activity \
    ComponentInfo{com.abc.xyz/com.abc.xyz.MapViews}: \
    android.view.InflateException: Binary XML file line #2: \
    Error inflating class fragment
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:123)
 at android.app.ActivityThread.main(ActivityThread.java:4627)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:521)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 at dalvik.system.NativeStart.main(Native Method)

Caused by: android.view.InflateException: Binary XML file line #2: \
    Error inflating class fragment
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
 at android.app.Activity.setContentView(Activity.java:1647)
 at com.abc.xyz.MapViews.onCreate(MapViews.java:12)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 ... 11 more

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader \
    dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:\
    /data/app/com.abc.xyz-2.apk]
 at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
 at android.view.LayoutInflater.createView(LayoutInflater.java:466)
 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
 at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 ... 19 more`
4

2 回答 2

3

如果您在旧设备(Honeycomb 之前)上使用 Fragment,则应始终将 Activity 从FragmentActivity.

因此,解决您的问题:将您的MapViews课程更改为 extend FragmentActivity

import android.support.v4.app.FragmentActivity;

public class MapViews extends FragmentActivity {
  ...

更多信息和完整示例: http ://android-er.blogspot.co.uk/2012/12/using-supportmapfragment.html

解释:

这里重要的例外是:java.lang.ClassNotFoundException: android.view.fragment.

这样做的原因是,在 Honeycomb 之前的设备上,当 Activity 尝试加载 xml 布局时,它会找到fragment它无法理解的关键字,因此它会尝试将其加载为简单的内置视图类 ( android.view.fragment)。

如果您扩展FragmentActivitysetContentView 方法将被覆盖,因此它可以处理fragment关键字。

于 2013-02-13T08:33:31.280 回答
0

你的布局有问题。只需尝试像这样定义 MapView :

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="Your Maps API Key goes here"
/>
于 2013-02-13T07:51:25.187 回答