Android地图基本应用程序崩溃

时间:2014-02-09 11:51:35

标签: android api maps

我正在尝试构建Google地图应用程序....我的行为与https://developers.google.com/maps/documentation/android/(hello map)中显示的相同。但是当我运行应用程序时它会崩溃。我不知道是什么导致了这个问题。我的代码是: 包com.googlemaps;

import java.io.IOException;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

private GoogleMap maps;

final static String URL = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  
    // Get a handle to the Map Fragment
   maps = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap();
   maps.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
   LatLng as = new LatLng(141.0819,37.092);
   maps.setMyLocationEnabled(true);
    maps.moveCamera(CameraUpdateFactory.newLatLngZoom(as, 13));
   maps.addMarker(new    MarkerOptions().title("PlaceName").snippet("Magnitude").position(as));


  }

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

<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

logcat的:

02-09 06:46:04.940: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:04.960: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:04.970: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:05.050: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:05.120: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:05.210: W/GooglePlayServicesUtil(1308): Google Play services out of date.  Requires 4132500 but found 4033530
02-09 06:46:05.210: D/AndroidRuntime(1308): Shutting down VM
02-09 06:46:05.210: W/dalvikvm(1308): threadid=1: thread exiting with uncaught exception (group=0xb4aceba8)
02-09 06:46:05.240: E/AndroidRuntime(1308): FATAL EXCEPTION: main
02-09 06:46:05.240: E/AndroidRuntime(1308): Process: com.googlemaps, PID: 1308
02-09 06:46:05.240: E/AndroidRuntime(1308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.googlemaps/com.googlemaps.MainActivity}: java.lang.NullPointerException
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.os.Looper.loop(Looper.java:136)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at java.lang.reflect.Method.invokeNative(Native Method)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at java.lang.reflect.Method.invoke(Method.java:515)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at dalvik.system.NativeStart.main(Native Method)
02-09 06:46:05.240: E/AndroidRuntime(1308): Caused by: java.lang.NullPointerException
02-09 06:46:05.240: E/AndroidRuntime(1308):     at com.googlemaps.MainActivity.onCreate(MainActivity.java:52)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.Activity.performCreate(Activity.java:5231)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-09 06:46:05.240: E/AndroidRuntime(1308):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-09 06:46:05.240: E/AndroidRuntime(1308):     ... 11 more
02-09 06:47:02.900: I/Process(1308): Sending signal. PID: 1308 SIG: 9

我认为问题出现在以下几个方面:

     maps.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
     LatLng as = new LatLng(141.0819,37.092);
     maps.setMyLocationEnabled(true);
    maps.moveCamera(CameraUpdateFactory.newLatLngZoom(as, 13));   
     maps.addMarker(new MarkerOptions().title("PlaceName").snippet("Magnitude").position(as));

如果删除了所有这些代码,应用程序就可以正常加载地图..但如果没有则会出现闪烁。

1 个答案:

答案 0 :(得分:0)

您是否拥有最新版本的Google服务? setMapType()是否会导致问题?它在oncreate的第52行说空指针。那是什么?