调用Google地图时,Android项目不会在设备中运行

时间:2014-12-22 11:36:52

标签: android eclipse android-support-library

我在设备中运行当前的android项目时遇到了一些麻烦。该项目没有构建错误(使用ecplise),但是当我运行时会发生此错误:

12-22 11:31:18.064: E/AndroidRuntime(10283): FATAL EXCEPTION: main
12-22 11:31:18.064: E/AndroidRuntime(10283): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.trocatela.com/br.trocatela.com.GoogleMapsActivity}: java.lang.NullPointerException
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.os.Looper.loop(Looper.java:176)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread.main(ActivityThread.java:5419)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at java.lang.reflect.Method.invokeNative(Native Method)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at java.lang.reflect.Method.invoke(Method.java:525)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at dalvik.system.NativeStart.main(Native Method)
12-22 11:31:18.064: E/AndroidRuntime(10283): Caused by: java.lang.NullPointerException
12-22 11:31:18.064: E/AndroidRuntime(10283):    at br.trocatela.com.GoogleMapsActivity.onCreate(GoogleMapsActivity.java:68)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.Activity.performCreate(Activity.java:5372)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
12-22 11:31:18.064: E/AndroidRuntime(10283):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
12-22 11:31:18.064: E/AndroidRuntime(10283):    ... 11 more

我的课是:

package br.trocatela.com;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

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 com.google.android.maps.OverlayItem;

public class GoogleMapsActivity extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        Intent intent = getIntent();
     String lat1 = intent.getStringExtra("lat");
    String longt = intent.getStringExtra("longt");

    LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String bestProvider = LM.getBestProvider(new Criteria(),true);
    //System.out.println("*************"+LM.getBestProvider(new Criteria(),true));
    Location l = LM.getLastKnownLocation(bestProvider);






    //Toast.makeText(this, "lat"+lat1, Toast.LENGTH_SHORT).show(); 
    //Toast.makeText(this, "longt"+longt, Toast.LENGTH_SHORT).show(); 


        // Displaying Zooming controls
        MapView mapView = (MapView) findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);

        /**
         * Changing Map Type
         * */
         mapView.setSatellite(true); // Satellite View
        // mapView.setStreetView(true); // Street View
        // mapView.setTraffic(true); // Traffic view

        /**
         * showing location by Latitude and Longitude
         * */        
        MapController mc = mapView.getController();




        double lat = Double.parseDouble(lat1);
        double lon = Double.parseDouble(longt);


        double lat2=l.getLatitude();
        double long2=l.getLongitude();

        Toast.makeText(this, "Encontra se a  "+ lat2+ "º de latitude", Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Encontra se a "+ long2+ "º de longitude", Toast.LENGTH_SHORT).show();

        GeoUtils g = new GeoUtils();
        GeoCoordinate g1 = new GeoCoordinate(lat, lon);
        GeoCoordinate g2 = new GeoCoordinate(lat2, long2);
      double dist=  g.geoDistanceInKm(g1, g2);

      Toast.makeText(this, "Voce Encontra se a  "+ dist+ "km do local pretendido", Toast.LENGTH_SHORT).show();




        GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
        mc.animateTo(geoPoint);
        mc.setZoom(15);
        mapView.invalidate(); 


        /**
         * Placing Marker
         * */
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
        AddItemizedOverlay itemizedOverlay = 
             new AddItemizedOverlay(drawable, this);


        OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

    }

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

map.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"
>
<com.google.android.maps.MapView
     android:id="@+id/map_view"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:enabled="true"
     android:clickable="true"
     android:apiKey="0CZ0aW7qB1Q__jlO6qkhl3IfFJTwGvwYUv7VDTQ"
/>
</LinearLayout>

类MainActivity放在我项目中的正确目录中(与其他项目一样,运行没有问题)。

任何人都可以看到我在这里缺少的东西吗?

1 个答案:

答案 0 :(得分:1)

检查LM.getLastKnownLocation(bestProvider)是否未返回null;