0

当我运行我的应用程序时,我收到此错误:

01-06 15:00:42.865: E/AndroidRuntime(24230): FATAL EXCEPTION: main
01-06 15:00:42.865: E/AndroidRuntime(24230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fotolife.app/com.fotolife.app.MainView}: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.os.Looper.loop(Looper.java:137)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at java.lang.reflect.Method.invokeNative(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at java.lang.reflect.Method.invoke(Method.java:511)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at dalvik.system.NativeStart.main(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.fotolife.app.Map.onCreateView(Map.java:12)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.Activity.performStart(Activity.java:5201)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
01-06 15:00:42.865: E/AndroidRuntime(24230):    ... 11 more
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.reflect.InvocationTargetException
01-06 15:00:42.865: E/AndroidRuntime(24230):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-06 15:00:42.865: E/AndroidRuntime(24230):    ... 24 more
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.google.android.maps.MapView.<init>(MapView.java:291)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.google.android.maps.MapView.<init>(MapView.java:264)
01-06 15:00:42.865: E/AndroidRuntime(24230):    at com.google.android.maps.MapView.<init>(MapView.java:247)
01-06 15:00:42.865: E/AndroidRuntime(24230):    ... 27 more

它说这个文件的第 12 行有错误:

package com.fotolife.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Map extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.map, container, false);
    }

}

第 12 行是:return inflater.inflate .....

这是片段应该膨胀的xml文件:

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

    <com.google.android.maps.MapView
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="@string/maps_api_key"
        />

</LinearLayout>
4

2 回答 2

1

这是你的错误:

01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

您不能MapView与片段一起使用,因为要使用片段,您的 Activity 需要是Activity(v11+) 或的子类FragmentActivity,但要使用MapView您的 Activity 必须是MapActivity.

如果您想同时使用地图和片段,则需要使用包含( v11MapFragment +) 或SupportMapFragment.

于 2013-01-06T20:12:59.937 回答
0

我以前从未使用过 Google Maps Android API,但查看您的堆栈跟踪:

01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

您的活动需要扩展MapActivity。如果您想使用片段,也许您应该使用新的 V2 API。https://developers.google.com/maps/documentation/android/

由于您的布局仅包含 a MapView,您是否考虑过使用 aSupportMapFragment代替:

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

或者

SupportMapFragment f = new SupportMapFragment();
getSupportFragmentManager().beingTransaction()
    .add(android.R.id.content, f)
    .commit();

请参阅文档:https ://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment

于 2013-01-06T20:10:21.347 回答