应用程序在加载显示当前位置和绘图线的地图时崩溃

时间:2011-11-29 03:42:12

标签: android

我正在尝试使用路径显示当前位置的地图。跑步时崩溃了。

我也检查了清单..但我不知道错误是什么..

PLease帮助我,我是android中的新蜜蜂..

这是我的代码:

MapActivity.java

package com.GoogleMaps;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */

 private MapView mapView;
 private LocationManager lm;
 private LocationListener ll;
 private MapController mc;
 GeoPoint p = null;
 Drawable defaultMarker = null;

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

    mapView = (MapView)findViewById(R.id.mapview);
    //show zoom in/out buttons
    mapView.setBuiltInZoomControls(true);
    //Standard view of the map(map/sat)
    mapView.setSatellite(false);
    //get controller of the map for zooming in/out
    mc = mapView.getController();
    // Zoom Level
    mc.setZoom(18); 

    // MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
    List<Overlay> list = mapView.getOverlays();
   //  list.add(myLocationOverlay);

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    ll = new MyLocationListener();

    lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,
            0,
            ll);

    //Get the current location in start-up
    GeoPoint initGeoPoint = new GeoPoint(
           (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
           (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
           mc.animateTo(initGeoPoint);
}

protected class MyLocationOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Paint paint = new Paint();

            super.draw(canvas, mapView, shadow);
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();

            mapView.getProjection().toPixels(p, myScreenCoords);

            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
            canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
            return true;
        }
    }

private class MyLocationListener implements LocationListener{

      public void onLocationChanged(Location argLocation) {
       // TODO Auto-generated method stub
       GeoPoint myGeoPoint = new GeoPoint((int)(argLocation.getLatitude()*1000000), (int)(argLocation.getLongitude()*1000000));

       Toast.makeText(getBaseContext(),
               "New location latitude [" +argLocation.getLatitude() +
               "] longitude [" + argLocation.getLongitude()+"]",
               Toast.LENGTH_SHORT).show();


       mc.animateTo(myGeoPoint);

      }

      public void onProviderDisabled(String provider) {
       // TODO Auto-generated method stub
      }

      public void onProviderEnabled(String provider) {
       // TODO Auto-generated method stub
      }

      public void onStatusChanged(String provider,
        int status, Bundle extras) {
       // TODO Auto-generated method stub
      }
     }    
protected boolean isRouteDisplayed() {
    return false;
}
}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   > 
 <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   >
    <TextView
     android:id="@+id/longitude"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="Longitude:"
  />
  <TextView
   android:id="@+id/latitude"
   android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="Latitude:"
  />
 </LinearLayout>
 <com.google.android.maps.MapView
  android:id="@+id/mapview"
  android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="false"
  android:apiKey="0dB02TjP-H_UqgsDpfxAhtsvGo5Z8cUJsTeHeDA"
 />
</LinearLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.GoogleMaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library  android:required="true" android:name="com.google.android.maps"/>
        <activity
            android:label="@string/app_name"
            android:name=".MapsActivity" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
  <uses-library android:name="com.google.android.maps" />
   </application>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-sdk android:minSdkVersion="8" />
</manifest>

logcat文件:

11-29 03:26:49.620: D/AndroidRuntime(671): Shutting down VM
11-29 03:26:49.620: W/dalvikvm(671): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-29 03:26:49.640: E/AndroidRuntime(671): FATAL EXCEPTION: main
11-29 03:26:49.640: E/AndroidRuntime(671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.GoogleMaps/com.GoogleMaps.MapsActivity}: java.lang.NullPointerException
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.os.Looper.loop(Looper.java:123)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 03:26:49.640: E/AndroidRuntime(671):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 03:26:49.640: E/AndroidRuntime(671):  at java.lang.reflect.Method.invoke(Method.java:521)
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-29 03:26:49.640: E/AndroidRuntime(671):  at dalvik.system.NativeStart.main(Native Method)
11-29 03:26:49.640: E/AndroidRuntime(671): Caused by: java.lang.NullPointerException
11-29 03:26:49.640: E/AndroidRuntime(671):  at com.GoogleMaps.MapsActivity.onCreate(MapsActivity.java:65)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 03:26:49.640: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 03:26:49.640: E/AndroidRuntime(671):  ... 11 more
11-29 03:26:52.320: I/Process(671): Sending signal. PID: 671 SIG: 9

1 个答案:

答案 0 :(得分:1)

您在MapsActivity类的第65行获得NullPointerException。你可以自己发布这条线吗?

可能的罪魁祸首是你不检查getLastLocation是否返回null,这是可能的。

使用空检查包围这些行

//Get the current location in start-up, if it exists
if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
    GeoPoint initGeoPoint = new GeoPoint(
        (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
        (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
     mc.animateTo(initGeoPoint);
}