遗憾的是,googlemap已经停止了“

时间:2014-01-14 13:52:15

标签: android eclipse

我正在尝试用api v2在android中创建一个地图。但是收到错误“遗憾的是googlemap已停止”

这是我的 MainActivity.java

package com.example.googlev2map;

import android.os.Bundle; 
import android.annotation.SuppressLint;
import android.app.Activity; 
import android.support.v4.app.FragmentActivity;
import android.view.Menu; enter code here
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 


public class MainActivity extends FragmentActivity {     

    private GoogleMap mMap;         
@Override     
protected void onCreate(Bundle savedInstanceState) {         
super.onCreate(savedInstanceState);         
setContentView(R.layout.activity_main);             
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();            
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);         
final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);         
Marker ciu = mMap.addMarker(new MarkerOptions()                                   
.position(CIU).title("My Office"));     
}     
@Override     
public boolean onCreateOptionsMenu(Menu menu) {         
// Inflate the menu; this adds items to the action bar if it is present.         
getMenuInflater().inflate(R.menu.main, menu);         
return true;     } } 

activity_main.xml中

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:paddingBottom="@dimen/activity_vertical_margin"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"    
android:paddingTop="@dimen/activity_vertical_margin"    
tools:context=".MainActivity" >
<TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="@string/hello_world" />
<fragment 
        android:id="@+id/map"     
        android:layout_width="match_parent"        
        android:layout_height="match_parent"        
class="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlev2map"
    android:versionCode="1"
    android:versionName="1.0" ><uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<permission
        android:name="com.example.googlev2map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"></permission>
    <uses-permission
        android:name="com.example.googlev2map.permission.MAPS_RECEIVE"/>
    <uses-permission
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission
        android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <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="AIzaSyAy2ThskwG3Q9ArpZR_GvJP5_5IWfcEJhU"/>
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version">
        </meta-data>
          <activity
            android:name="com.example.googlev2map.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>
    </application>
</manifest>

我的logcat显示

01-14 14:32:39.912: E/AndroidRuntime(923):  at android.os.Looper.loop(Looper.java:137)
01-14 14:32:39.912: E/AndroidRuntime(923):  at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 14:32:39.912: E/AndroidRuntime(923):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 14:32:39.912: E/AndroidRuntime(923):  at java.lang.reflect.Method.invoke(Method.java:525)
01-14 14:32:39.912: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 14:32:39.912: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 14:32:39.912: E/AndroidRuntime(923):  at dalvik.system.NativeStart.main(Native Method)
01-14 14:32:39.912: E/AndroidRuntime(923): Caused by: java.lang.NullPointerException
01-14 14:32:39.912: E/AndroidRuntime(923):  at com.example.googlev2map.MainActivity.onCreate(MainActivity.java:25)
01-14 14:32:39.912: E/AndroidRuntime(923):  at android.app.Activity.performCreate(Activity.java:5133)
01-14 14:32:39.912: E/AndroidRuntime(923):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-14 14:32:39.912: E/AndroidRuntime(923):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-14 14:32:39.912: E/AndroidRuntime(923):  ... 11 more

我也导入了google_play_services_lib,并将其添加到我的项目中。

2 个答案:

答案 0 :(得分:1)

您需要更改此

 class="com.google.android.gms.maps.MapFragment"

 class="com.google.android.gms.maps.SupportMapFragment"

并改变

 mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

因为你有

android:minSdkVersion="8" in manifest

答案 1 :(得分:0)

您的问题是您尝试将MapFragment转换为支持片段查看日志中的ClassCastException。