标签:
整了四五天显示的地图终于不是北京了,心酸泪一把T~T。
java.lang.NullPointerException 是空指针异常,问候了度娘,又看了几遍自己的代码都没觉得有异常, 突发奇想的改了改代码竟然成了。。。。。
(下面是改完后的代码)
1 public class Local_MainActivity extends Activity { 2 private MapView mapView; 3 private BaiduMap baiduMap; 4 private LocationManager locationManager; 5 private String provider; 6 private boolean isFristLoacte=true; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 SDKInitializer.initialize(getApplicationContext()); 12 setContentView(R.layout.local); 13 14 //百度地图 15 16 this.mapView = (MapView) findViewById(R.id.map_view); 17 this.baiduMap = this.mapView.getMap(); 18 this.locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 19 //获取所有可用的地址提供器 20 List<String> providerList; 21 providerList=this.locationManager.getProviders(true); 22 if (providerList.contains(LocationManager.NETWORK_PROVIDER)) { 23 provider = LocationManager.NETWORK_PROVIDER; 24 System.out.println(); 25 System.out.println("network!!!!!!!!!!!!!!!!!"); 26 } else if (providerList.contains(LocationManager.GPS_PROVIDER)) { 27 this.provider = LocationManager.GPS_PROVIDER; 28 System.out.println(); 29 System.out.println("GPS!!!!!!!!!!!!!!!!!"); 30 } 31 else { 32 Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show(); 33 return; 34 } 35 36 try { 37 Location location = this.locationManager.getLastKnownLocation(this.provider); 38 // location = this.locationManager.getLastKnownLocation(this.locationManager.GPS_PROVIDER); 39 if (location != null) { 40 navigateTo(location); 41 } 42 this.locationManager.requestLocationUpdates(provider, 5000, 1, locationListener); 43 }catch(Exception e){ 44 System.out.println(e); 45 } 46 47 } 48 /* @Override 49 protected void onDestroy(){ 50 super.onDestroy();ffffr 51 mapView.onDestroy(); 52 } 53 @Override 54 protected void onPause(){ 55 super.onPause(); 56 mapView.onPause(); 57 } 58 @Override 59 protected void onResume(){ 60 super.onResume(); 61 mapView.onResume(); 62 }*/ 63 64 private void navigateTo(Location location){ 65 if (isFristLoacte){ 66 LatLng ll=new LatLng(location.getLatitude(),location.getLongitude()); 67 MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll); 68 baiduMap.animateMapStatus(update); 69 update=MapStatusUpdateFactory.zoomTo(16f); 70 baiduMap.animateMapStatus(update); 71 isFristLoacte=false; 72 } 73 } 74 75 @Override 76 public boolean onCreateOptionsMenu(Menu menu) { 77 // Inflate the menu; this adds items to the action bar if it is present. 78 getMenuInflater().inflate(R.menu.menu_main, menu); 79 return true; 80 } 81 82 @Override 83 public boolean onOptionsItemSelected(MenuItem item) { 84 // Handle action bar item clicks here. The action bar will 85 // automatically handle clicks on the Home/Up button, so long 86 // as you specify a parent activity in AndroidManifest.xml. 87 int id = item.getItemId(); 88 89 //noinspection SimplifiableIfStatement 90 if (id == R.id.action_settings) { 91 return true; 92 } 93 94 return super.onOptionsItemSelected(item); 95 } 96 LocationListener locationListener=new LocationListener() { 97 @Override 98 public void onLocationChanged(Location location) { 99 if (location!=null){ 100 navigateTo(location); 101 } 102 } 103 104 @Override 105 public void onStatusChanged(String provider, int status, Bundle extras) { 106 107 } 108 109 @Override 110 public void onProviderEnabled(String provider) { 111 112 } 113 114 @Override 115 public void onProviderDisabled(String provider) { 116 117 } 118 }; 119 @Override 120 protected void onDestroy(){ 121 super.onDestroy(); 122 mapView.onDestroy(); 123 if(this.locationManager!=null){ 124 try{ 125 this.locationManager.removeUpdates(locationListener); 126 }catch(Exception ex){} 127 128 } 129 } 130 @Override 131 protected void onPause(){ 132 super.onPause(); 133 mapView.onPause(); 134 } 135 @Override 136 protected void onResume(){ 137 super.onResume(); 138 mapView.onResume(); 139 } 140 141 }
之前是因为22-33的代码是这样写的
if (providerList.contains(LocationManager.GPS_PROVIDER)) { this.provider = LocationManager.GPS_PROVIDER; System.out.println(); System.out.println("GPS!!!!!!!!!!!!!!!!!"); }else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
System.out.println();
System.out.println("network!!!!!!!!!!!!!!!!!");
} else
else {
Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
return;
}
if跟else if 对调了以下
不要问我为什么
反正就是行了
百度地图定位 java.lang.NullPointerException
标签:
原文地址:http://www.cnblogs.com/Eliosa/p/5074323.html