实现谷歌地图片段时出错

时间:2013-11-20 10:12:00

标签: android google-maps android-fragments

我想向用户显示TabActivity上的地图。标签上没有错误。我有4个tabActivities,其中一个将包含一张地图。

我的布局文件;

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

    <fragment
        android:id="@+id/myMapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
         />
</RelativeLayout>

带有导入的我的类文件;

package com.mypackage;

import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

public class Main extends FragmentActivity{

private GoogleMap mMap;

UiSettings settings;
MapController mapController;
GeoPoint geopoint;
RectF oval;
Canvas canvas;
int mRadius = 5;

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

    setUpMap();
}

@Override
protected void onResume(){
    super.onResume();
    setUpMap();
}

private void setUpMap(){
    if (mMap != null) {
        return;
    }
    mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap();
    if (mMap == null) {
        return;
    }
    final LatLng place1  = new LatLng(40,40);

    mMap.addMarker(new MarkerOptions()
    .position(place1)
    .title("blabla")
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place1, 15));
    mMap.setTrafficEnabled(true);
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
}

我的清单文件(仅限于mapclass和mapsupport);

<permission
        android:name="com.mypackage.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.mypackage.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<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" >

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

    <activity
        android:name="SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="Main" 
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">   
    </activity>
</application>

修改 最后LogCat输出;

11-21 08:34:54.179: E/AndroidRuntime(27297): FATAL EXCEPTION: main
11-21 08:34:54.179: E/AndroidRuntime(27297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.Main}:    android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1499)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.widget.TabHost.setCurrentTab(TabHost.java:348)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:141)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.View.performClick(View.java:2485)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.View$PerformClick.run(View.java:9080)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.os.Handler.handleCallback(Handler.java:587)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.os.Looper.loop(Looper.java:130)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.ActivityThread.main(ActivityThread.java:3768)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at java.lang.reflect.Method.invoke(Method.java:507)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at dalvik.system.NativeStart.main(Native Method)
11-21 08:34:54.179: E/AndroidRuntime(27297): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.Activity.setContentView(Activity.java:1657)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at com.mypackage.Main.onCreate(AdresTab.java:33)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-21 08:34:54.179: E/AndroidRuntime(27297):    ... 18 more
11-21 08:34:54.179: E/AndroidRuntime(27297): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.mypackage-1.apk]
11-21 08:34:54.179: E/AndroidRuntime(27297):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-21 08:34:54.179: E/AndroidRuntime(27297):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
11-21 08:34:54.179: E/AndroidRuntime(27297):    ... 27 more

还有其他有用的想法吗?

2 个答案:

答案 0 :(得分:2)

使用以下代码:

GoogleMap mMap;
SupportMapFragment mapFrag;

现在设置它:

if (mMap == null) {
            mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.myMapView);
            mMap =  mapFrag.getMap();

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                // The Map is verified. It is now safe to manipulate the map.
                 AddMarkers();
            }
        }

还要在清单文件中添加此元标记。

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

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

答案 1 :(得分:0)

我解决了我的问题。我的布局文件中的片段类无法从google_play_services库中找到。我忘了将这个库的jar文件添加到我的项目中。我只从java构建路径添加该项目。我必须从 Project-&gt; Properties-&gt; Android-&gt; Library 添加此lib。这解决了。不管怎样,谢谢。