刚刚接触 android ...菜鸟级菜鸟...刚运行就显示:unfortunately,GpsExample has stopped..
不知道怎么整...package com.example.gpsexample;import java.io.IOException;
import java.util.List;
import java.util.Locale;
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.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class GpsExample extends MapActivity {
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
public static final int ZOOM_IN = Menu.FIRST;
public static final int ZOOM_OUT = Menu.FIRST + 1;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
myMapView = (MapView)findViewById(R.id.mapView1);
mapController = myMapView.getController();
//设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
//设置缩放控制,这里使用自己实现的缩放菜单
myMapView.displayZoomControls(false);
//设置使用MyLocationOverlay绘图
mapController.setZoom(17);
myPosition = new MyLocationOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(myPosition);
//设置Criteria(服务商)的信息
Criteria criteria = new Criteria();
//经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得最好效果的criteria
String provider = locationManager.getBestProvider(criteria, true);
//获得坐标相应信息
Location location = locationManager.getLastKnownLocation(provider);
//更新坐标相关信息
updateWithNewLocation(location);
//注册一个周期的更新,3000ms更新一次
//locationManager用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
}
private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id.textView1);
String addressString = "没有找到地址\n";
if(location != null){
//为绘制标志的类设置坐标
//myPosition.
//取得经度和纬度
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
//定位到指定坐标
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "经度:" + lat + "\n纬度:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
//根据地理环境来确定编码
Geocoder gc = new Geocoder(this, Locale.getDefault());
try{
//取得地址相关的一些信息、经度、纬度
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size() > 0){
Address address = addresses.get(0);
for(int i = 0; i < address.getMaxAddressLineIndex(); i++){
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString = sb.toString();
}
}
}catch(IOException e){}
}else{
latLongString = "没有找到坐标. \n";
}
myLocationText.setText("你当前的坐标如下:\n" + latLongString + "\n" + addressString);
}
private final LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {//Provider转态在可用、暂时不可服务和无服务三个状态直接切换时触发此函数
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {//Provider启用时触发此函数,比如GPS被打开
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {//Provider禁用时触发此函数,比如GPS被关闭
// TODO Auto-generated method stub
updateWithNewLocation(null);
}
public void onLocationChanged(Location location) {//当坐标改变时触发事件
// TODO Auto-generated method stub
updateWithNewLocation(location);
}
};
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case ZOOM_IN:
mapController.zoomIn();
return true;
case ZOOM_OUT:
mapController.zoomOut();
return true;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, "放大");
menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");
return true;
}
class MyLocationOverlay extends Overlay{
Location mLocation;
//更新坐标时,设置该坐标,以便画图
public void setLocation(Location location){
mLocation = location;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow, when);
Paint paint = new Paint();
Point myScreenCoords = new Point();
//将经纬度转换成实际屏幕坐标
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude() * 1E6), (int)(mLocation.getLongitude() * 1E6));
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".GpsExample"
android:label="@string/title_activity_gps_example" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
不知道怎么整...package com.example.gpsexample;import java.io.IOException;
import java.util.List;
import java.util.Locale;
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.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class GpsExample extends MapActivity {
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
public static final int ZOOM_IN = Menu.FIRST;
public static final int ZOOM_OUT = Menu.FIRST + 1;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
myMapView = (MapView)findViewById(R.id.mapView1);
mapController = myMapView.getController();
//设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
//设置缩放控制,这里使用自己实现的缩放菜单
myMapView.displayZoomControls(false);
//设置使用MyLocationOverlay绘图
mapController.setZoom(17);
myPosition = new MyLocationOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(myPosition);
//设置Criteria(服务商)的信息
Criteria criteria = new Criteria();
//经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得最好效果的criteria
String provider = locationManager.getBestProvider(criteria, true);
//获得坐标相应信息
Location location = locationManager.getLastKnownLocation(provider);
//更新坐标相关信息
updateWithNewLocation(location);
//注册一个周期的更新,3000ms更新一次
//locationManager用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
}
private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id.textView1);
String addressString = "没有找到地址\n";
if(location != null){
//为绘制标志的类设置坐标
//myPosition.
//取得经度和纬度
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
//定位到指定坐标
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "经度:" + lat + "\n纬度:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
//根据地理环境来确定编码
Geocoder gc = new Geocoder(this, Locale.getDefault());
try{
//取得地址相关的一些信息、经度、纬度
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size() > 0){
Address address = addresses.get(0);
for(int i = 0; i < address.getMaxAddressLineIndex(); i++){
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString = sb.toString();
}
}
}catch(IOException e){}
}else{
latLongString = "没有找到坐标. \n";
}
myLocationText.setText("你当前的坐标如下:\n" + latLongString + "\n" + addressString);
}
private final LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {//Provider转态在可用、暂时不可服务和无服务三个状态直接切换时触发此函数
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {//Provider启用时触发此函数,比如GPS被打开
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {//Provider禁用时触发此函数,比如GPS被关闭
// TODO Auto-generated method stub
updateWithNewLocation(null);
}
public void onLocationChanged(Location location) {//当坐标改变时触发事件
// TODO Auto-generated method stub
updateWithNewLocation(location);
}
};
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case ZOOM_IN:
mapController.zoomIn();
return true;
case ZOOM_OUT:
mapController.zoomOut();
return true;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, "放大");
menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");
return true;
}
class MyLocationOverlay extends Overlay{
Location mLocation;
//更新坐标时,设置该坐标,以便画图
public void setLocation(Location location){
mLocation = location;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow, when);
Paint paint = new Paint();
Point myScreenCoords = new Point();
//将经纬度转换成实际屏幕坐标
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude() * 1E6), (int)(mLocation.getLongitude() * 1E6));
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".GpsExample"
android:label="@string/title_activity_gps_example" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
< uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION" /> 把你的报错信息贴出来,GPS应该在室外测试才能得到数据。