我正在使用此代码查找当前位置。
我的叠加功能正常但我无法获得当前的经度和纬度。获取用户的当前位置后,应用程序将关闭“应用程序已意外停止”消息。
我已经学过一些关于寻找位置的教程,但我无法解决它。
package google.com;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import java.util.*;
import com.google.android.maps.Overlay;
import android.location.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuItem;
public class nandyog extends MapActivity implements LocationListener{
LocationManager locManager;
Location location;
double lat;
double lng;
Boolean network_enabled;
Boolean gps_enabled;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
MapView mapview = (MapView) findViewById(R.id.mapview);
mapview.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapview.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.iconr);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = locManager.getLastKnownLocation(locManager.NETWORK_PROVIDER);
lat = loc.getLatitude();
lng = loc.getLongitude();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "Current Location","LAL-DARWAJA");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_map, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
lat = location.getLatitude();
lng = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
/*
final class GPoint extends GeoPoint {
public GPoint(double latitude, double longitude) {
super((int) (latitude * 1E6), (int) (longitude * 1E6));
}
}
*/
我的清单文件在
下面<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="google.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".nandyog"
android:label="@string/title_activity_map"
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>
Logcat错误是......
08-31 18:55:49.226:D / dalvikvm(368):GC_CONCURRENT释放1164K,54%免费3148K / 6727K,外部1625K / 2137K,暂停6ms + 5ms 08-31 18:55:49.536:D / dalvikvm(368):GC_CONCURRENT释放529K,51%免费3307K / 6727K,外部1625K / 2137K,暂停6ms + 6ms 08-31 18:55:50.065:D / AndroidRuntime(368):关闭VM 08-31 18:55:50.076:W / dalvikvm(368):threadid = 1:线程退出未捕获异常(组= 0x40015560) 08-31 18:55:50.105:E / AndroidRuntime(368):致命异常:主要 08-31 18:55:50.105:E / AndroidRuntime(368):java.lang.RuntimeException:无法启动活动ComponentInfo {google.com/google.com.nandyog}:java.lang.NullPointerException 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.os.Handler.dispatchMessage(Handler.java:99) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.os.Looper.loop(Looper.java:130) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread.main(ActivityThread.java:3683) 08-31 18:55:50.105:E / AndroidRuntime(368):at java.lang.reflect.Method.invokeNative(Native Method) 08-31 18:55:50.105:E / AndroidRuntime(368):at java.lang.reflect.Method.invoke(Method.java:507) 08-31 18:55:50.105:E / AndroidRuntime(368):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 08-31 18:55:50.105:E / AndroidRuntime(368):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-31 18:55:50.105:E / AndroidRuntime(368):at dalvik.system.NativeStart.main(Native Method) 08-31 18:55:50.105:E / AndroidRuntime(368):引起:java.lang.NullPointerException 08-31 18:55:50.105:E / AndroidRuntime(368):google.com.nandyog.onCreate(nandyog.java:46) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-31 18:55:50.105:E / AndroidRuntime(368):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 08-31 18:55:50.105:E / AndroidRuntime(368):... 11更多 08-31 18:55:50.365:D / dalvikvm(368):GC_CONCURRENT释放838K,54%免费3119K / 6727K,外部1986K / 2137K,暂停4ms + 32ms 08-31 18:56:09.355:I / Process(368):发送信号。 PID:368 SIG:9
答案 0 :(得分:0)
您是否尝试从此获取布尔值:
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
您应该以静态方式访问NETWORK_PROVIDER
,如下所示
Location loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
不
Location loc = locManager.getLastKnownLocation(locManager.NETWORK_PROVIDER);