什么似乎是在我运行我的应用程序的每个SGS3设备上粉碎的原因? 这是唯一无法运行的设备(4.1.2 OS版本)。 我试过:LG optimus one + black,N4,HTC HD2,SGS 1 + 2 ..等等...
在Manifest文件中,我做了:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
那些是最相关的行注册。 Manifest.xml(针对我的问题)。
onCreate方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to MapView
mapView = (MapView) findViewById(R.id.map_view);
tvLocation = (TextView)findViewById(R.id.tv_location);
mapView.setKeepScreenOn(true);
// Setting Zoom Controls on MapView
mapView.setBuiltInZoomControls(true);
mapView.setTraffic(true);
mapView.setSatellite(false);
ImageButton settings = (ImageButton)findViewById(R.id.settingsButton);
settings.setOnClickListener(this);
Button helpme = (Button)findViewById(R.id.helpMeButton); //find Button and gives a name: 'help'
ToggleButton mapExchangeToggleButton = (ToggleButton)findViewById(R.id.mapExchangeToggleButton);
helpme.setOnClickListener(this); //register Button as an OnClicklistener
mapExchangeToggleButton.setOnClickListener(this);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
onResume方法:
public void onResume(){
super.onResume();
SharedPreferences pre = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
phoneNumber = pre.getString("phone","" );
}
LogCat是:
03-05 20:39:49.910: D/dalvikvm(27589): GC_CONCURRENT freed 173K, 9% free 12425K/13511K, paused 3ms+1ms, total 18ms
03-05 20:39:49.910: D/dalvikvm(27589): WAIT_FOR_CONCURRENT_GC blocked 6ms
03-05 20:39:49.910: W/CursorWrapperInner(27589): Cursor finalized without prior close()
03-05 20:39:49.965: D/dalvikvm(27589): GC_FOR_ALLOC freed 6K, 8% free 12814K/13895K, paused 14ms, total 14ms
03-05 20:39:49.990: W/System.err(27589): java.io.IOException: Service not Available
03-05 20:39:49.995: W/System.err(27589): at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-05 20:39:49.995: W/System.err(27589): at in.wptrafficanalyzer.locationingooglemap.MainActivity.onLocationChanged(MainActivity.java:122)
03-05 20:39:49.995: W/System.err(27589): at in.wptrafficanalyzer.locationingooglemap.MainActivity.onCreate(MainActivity.java:83)
03-05 20:39:49.995: W/System.err(27589): at android.app.Activity.performCreate(Activity.java:5206)
03-05 20:39:49.995: W/System.err(27589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-05 20:39:49.995: W/System.err(27589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-05 20:39:49.995: W/System.err(27589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-05 20:39:49.995: W/System.err(27589): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-05 20:39:49.995: W/System.err(27589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-05 20:39:49.995: W/System.err(27589): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 20:39:49.995: W/System.err(27589): at android.os.Looper.loop(Looper.java:137)
03-05 20:39:49.995: W/System.err(27589): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-05 20:39:49.995: W/System.err(27589): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 20:39:49.995: W/System.err(27589): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 20:39:49.995: W/System.err(27589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-05 20:39:49.995: W/System.err(27589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-05 20:39:50.000: W/System.err(27589): at dalvik.system.NativeStart.main(Native Method)
03-05 20:39:50.000: D/AndroidRuntime(27589): Shutting down VM
03-05 20:39:50.000: W/dalvikvm(27589): threadid=1: thread exiting with uncaught exception (group=0x419242a0)
03-05 20:39:50.000: E/AndroidRuntime(27589): FATAL EXCEPTION: main
03-05 20:39:50.000: E/AndroidRuntime(27589): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationingooglemap/in.wptrafficanalyzer.locationingooglemap.MainActivity}: java.lang.NullPointerException
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.os.Looper.loop(Looper.java:137)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-05 20:39:50.000: E/AndroidRuntime(27589): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 20:39:50.000: E/AndroidRuntime(27589): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 20:39:50.000: E/AndroidRuntime(27589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-05 20:39:50.000: E/AndroidRuntime(27589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-05 20:39:50.000: E/AndroidRuntime(27589): at dalvik.system.NativeStart.main(Native Method)
03-05 20:39:50.000: E/AndroidRuntime(27589): Caused by: java.lang.NullPointerException
03-05 20:39:50.000: E/AndroidRuntime(27589): at in.wptrafficanalyzer.locationingooglemap.MainActivity.onLocationChanged(MainActivity.java:128)
03-05 20:39:50.000: E/AndroidRuntime(27589): at in.wptrafficanalyzer.locationingooglemap.MainActivity.onCreate(MainActivity.java:83)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.Activity.performCreate(Activity.java:5206)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-05 20:39:50.000: E/AndroidRuntime(27589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-05 20:39:50.000: E/AndroidRuntime(27589): ... 11 more
onLocationChanged():
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
);
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getAddressLine(1);
country = addresses.get(0).getAddressLine(2);
tvLocation.setText("Street Address: " + address+ "\nCity: "+city +"\nCountry: " +country);
GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));
MapController mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(15);
mapView.invalidate();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);
OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);
currentLocationOverlay.addOverlay(currentLocation);
mapOverlays.clear();
mapOverlays.add(currentLocationOverlay);
}
答案 0 :(得分:0)
你正在捕捉并打印:
java.io.IOException: Service not Available
表示地址永远不会被设置为null:
List<Address> addresses = null;
因此当您尝试访问该列表中的项目时,您可能会收到NullPointerException:
address = addresses.get(0).getAddressLine(0);
看起来android.location.Geocoder在该特定设备上不可用。我会检查权限等内容,并启用/启用GPS。