-1

我正在为 android 开发一个基于地图的应用程序,并处于检测当前位置的最后阶段。但现在我的应用程序无法正常工作。它既不能安装在模拟器中,也不能安装在真实设备中。每当我尝试时,我都会在控制台中收到一条消息,上面写着:

无法安装应用程序 (map.apk)。

这是我的日志:

01-14 18:08:20.576: D/dalvikvm(342): GC_FOR_MALLOC freed 4859 objects / 301296 bytes in 100ms
01-14 18:08:20.916: D/dalvikvm(342): GC_FOR_MALLOC freed 10156 objects / 616976 bytes in 96ms
01-14 18:08:21.216: D/dalvikvm(342): GC_FOR_MALLOC freed 4664 objects / 306384 bytes in 88ms
01-14 18:08:21.576: D/dalvikvm(342): GC_FOR_MALLOC freed 6137 objects / 374304 bytes in 152ms
01-14 18:08:21.956: D/dalvikvm(342): GC_FOR_MALLOC freed 8481 objects / 648904 bytes in 98ms
01-14 18:08:22.266: D/dalvikvm(342): GC_FOR_MALLOC freed 6080 objects / 370008 bytes in 98ms
01-14 18:08:22.407: D/AndroidRuntime(342): Shutting down VM
01-14 18:08:22.407: W/dalvikvm(342): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-14 18:08:22.427: E/AndroidRuntime(342): FATAL EXCEPTION: main
01-14 18:08:22.427: E/AndroidRuntime(342): java.lang.RuntimeException: Unable to start activity ComponentInfo{g.g.g/g.g.g.GPSLocatorActivity}: java.lang.NullPointerException
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-14 18:08:22.427: E/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:08:22.427: E/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:521)
01-14 18:08:22.427: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-14 18:08:22.427: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-14 18:08:22.427: E/AndroidRuntime(342):  at dalvik.system.NativeStart.main(Native Method)
01-14 18:08:22.427: E/AndroidRuntime(342): Caused by: java.lang.NullPointerException
01-14 18:08:22.427: E/AndroidRuntime(342):  at g.g.g.GPSLocatorActivity.onCreate(GPSLocatorActivity.java:50)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-14 18:08:22.427: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-14 18:08:22.427: E/AndroidRuntime(342):  ... 11 more

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.mapView);
      mapView.setTraffic(true);
      mapView.setBuiltInZoomControls(true);



      List<Overlay> mapOverlays = mapView.getOverlays();
      Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
      HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
      GeoPoint point = new GeoPoint(
              (int) (location.getLatitude() * 1E6), 
              (int) (location.getLongitude() * 1E6));

          OverlayItem overlayitem = new OverlayItem(point,"","" );
      itemizedoverlay.addOverlay(overlayitem);
      mapOverlays.add(itemizedoverlay);
      }

      @Override
      protected boolean isRouteDisplayed()
      {
      return false;
      }
4

1 回答 1

3

可能是您的位置检测器引起的问题。试试这个以知道“位置”是否为空

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.mapView);
      mapView.setTraffic(true);
      mapView.setBuiltInZoomControls(true);



      List<Overlay> mapOverlays = mapView.getOverlays();
      Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
      HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

       //see here;
      location = getGpsLocation();
      if(location!=null){
          GeoPoint point = new GeoPoint(
              (int) (location.getLatitude() * 1E6), 
              (int) (location.getLongitude() * 1E6));

          OverlayItem overlayitem = new OverlayItem(point,"","" );
          itemizedoverlay.addOverlay(overlayitem);
          mapOverlays.add(itemizedoverlay);
      }else{
         Toast.makeText(getApplicationContext(), "GPS error", Toast.LENGTH_LONG).show();
      }
  }

  //see here;
  public Location getGpsLocation()
  {
          Location res = new Location("");
          res.setLatitude(0);
      res.setLongitude(0);

          // res = ; try some code to find you your current location

          return res;
  }
  @Override
  protected boolean isRouteDisplayed()
  {
  return false;
  }
于 2013-01-14T13:36:19.130 回答