开发者

Why does this map program "stop unexpectedly"?

开发者 https://www.devze.com 2023-03-22 21:04 出处:网络
I am trying to edit the google maps from the android developers HelloGoogleMaps tutorial combined with locationlistener and locationmanager so that the geopoint(latitude, longitude) that the overlay i

I am trying to edit the google maps from the android developers HelloGoogleMaps tutorial combined with locationlistener and locationmanager so that the geopoint(latitude, longitude) that the overlay item would be placed at my current location. I am an extreme beginner in android so please help me with simple language!! xD

My SampleMap.java file:

    package com.example.samplemap;

        import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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


        public class SampleMap extends MapActivity {
        /** Called when the activity is first created. */
        Location location;  
        @Override
        protected boolean isRouteDisplayed() {
        return false;
        }

        double lat = location.getLatitude();
        double lng = location.getLongitude();

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

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

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

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);

        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
        GeoPoint point = new GeoPoint((int) (lat), (int) (lng));
        OverlayItem overlayitem = new OverlayItem(point, "", "");

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

        }

        private class mylocationlistener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
                if (location != null) {
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                Toast.makeText(SampleMap.this,
                    location.getLatitude() + " " + location.getLongitude(),
                    Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            }

        }

and my HelloItemizedOverly.java

package com.example.samplemap;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}

and manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.samplemap"
        android:versionCode="1"
        android:versionName="1.0">
        <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">   </uses-   permission>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
        <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />

        <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />

        <activity android:name=".SampleMap"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>

        </application>
        </manifest>

Can you also give me the permission code I need??? >.<

DDMS log cat:
07-26 13:53:27.077: DEBUG/EAS SyncManager(2258): !!! EAS SyncManager, onCreate
07-26 13:53:27.171: DEBUG/Eas Debug(2258): Logging: 
07-26 13:53:27.179: DEBUG/EAS SyncManager(2258): !!! EAS SyncManager, onDestroy
07-26 13:53:28.296: DEBUG/dalvikvm(2258): GC_FOR_MALLOC freed 3927 objects / 265008 bytes in 44ms
07-26 13:53:28.890: DEBUG/dalvikvm(1699): GC_EXPLICIT freed 265 objects / 15568 bytes in 86ms
07-26 13:53:29.429: DEBUG/Email(2258): *** synchronizeMailboxGeneric ***
07-26 13:53:30.398: INFO/ActivityManager(1103): No longer want com.google.android.apps.uploader (pid 2084): hidden #16
07-26 13:53:30.421: INFO/UsageStats(1103): Something wrong here, didn't expect com.example.samplemap to be paused
07-26 13:53:30.925: WARN/ActivityManager(1103): Activity pause timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}
07-26 13:53:33.882: DEBUG/dalvikvm(1187): GC_EXPLICIT freed 1914 objects / 112528 bytes in 75ms
07-26 13:53:38.874: DEBUG/dalvikvm(2011): GC_EXPLICIT freed 91 objects / 8040 bytes in 69ms
07-26 13:53:40.426: WARN/ActivityManager(1103): Activity idle timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}
07-26 13:53:45.499: DEBUG/dalvikvm(2019): GC_EXPLICIT freed 304 objects / 17824 bytes in 76ms
07-26 13:53:50.491: DEBUG/dalvikvm(2074): GC_EXPLICIT freed 135 objects / 6720 bytes in 66ms
07-26 13:53:55.507: DEBUG/dalvikvm(1560): GC_EXPLICIT freed 65 objects / 3896 bytes in 75ms
07-26 13:54:05.132: DEBUG/dalvikvm(1726): GC_EXPLICIT freed 46 objects / 2216 bytes in 54ms
07-26 13:54:10.171: DEBUG/dalvikvm(1445): GC_EXPLICIT freed 268 objects / 9960 bytes in 94ms
07-26 13:54:15.156: DEBUG/dalvikvm(1215): GC_EXPLICIT freed 1011 objects / 51888 bytes in 75ms
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): wakeWhenReadyLocked(26)
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): handleWakeWhenReady(26)
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): pokeWakelock(5000)
07-26 13:54:16.804: INFO/power(1103): *** set_screen_state 1
07-26 13:54:16.829: DEBUG/Sensors(1103): using sensors (name=sensors)
07-26 13:54:16.851: INFO/UsageStats(1103): Unexpected resume of com.example.samplemap while already resumed in com.example.samplemap
07-26 13:54:16.882: DEBUG/WifiService(1103): ACTION_SCREEN_ON
07-26 13:54:17.124: DEBUG/dalvikvm(1184): GC_FOR_MALLOC freed 11174 objects / 598888 bytes in 105ms
07-26 13:54:17.382: DEBUG/SurfaceFlinger(1103): Screen about to return, flinger = 0x120f38
07-26 13:54:19.085: DEBUG/WindowManager(1103): I'm tired mEndcallBehavior=0x2
07-26 13:54:19.328: INFO/power(1103): *** set_screen_state 0
07-26 13:54:19.341: DEBUG/SurfaceFlinger(1103): About to give-up screen, flinger = 0x120f38
07-26 13:54:19.359: DEBUG/Sensors(1103): using accelerometer (name=accelerometer)
07-26 13:54:19.374: DEBUG/WifiService(1103): ACTION_SCREEN_OFF
07-26 13:54:19.382: DEBUG/WifiService(1103): setting ACTION_DEVICE_IDLE timer for 900000ms
07-开发者_运维知识库26 13:54:19.870: WARN/ActivityManager(1103): Activity pause timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}


Well for one thing it looks like you are trying to access the location variable before it is initialized, since you lon/lat and location vars are class scoped they will get initialized before the location is actually initialized (if it ever is)

double lat = location.getLatitude(); // location is never set, it will be null! 
double lng = location.getLongitude();

As a matter of fact I don't even see you initializing location (you have parameter variable of the same name in your update, but I can't see you moving that to the class scoped version)

public void onLocationChanged(Location location) { /

Even if you did something like

this.location = new Location(location);

Inside the listener you don't know when this will be created (or even if)

Also MyLocationOverlay is a built in overlay for Google Maps that will handle all of that for you.

Update Drop the location var near the top

Change

 double lat = location.getLatitude();
 double lng = location.getLongitude();

to

 double lat = 0;
 double lng = 0;

then inside your location listener drop the double, you are just creating a local if you do that.

 lat = location.getLatitude();
 lng = location.getLongitude();

That will at least take care of that issue, again I would look at the link for the MyLocationOverlay, it takes care of everything for you in regards to current location

0

精彩评论

暂无评论...
验证码 换一张
取 消