Android 类搜索函数Mapview
这是我的项目,您可以在列表视图中单击医院名称,并在下一个活动中显示详细信息,详细信息下方是地图视图。我的项目崩溃了,我从logcat那里得到了这个错误Android 类搜索函数Mapview,android,google-maps,geocoding,Android,Google Maps,Geocoding,这是我的项目,您可以在列表视图中单击医院名称,并在下一个活动中显示详细信息,详细信息下方是地图视图。我的项目崩溃了,我从logcat那里得到了这个错误 11-30 18:22:50.085: E/AndroidRuntime(13114): FATAL EXCEPTION: main 11-30 18:22:50.085: E/AndroidRuntime(13114): java.lang.NullPointerException 11-30 18:22:50.085: E/AndroidRu
11-30 18:22:50.085: E/AndroidRuntime(13114): FATAL EXCEPTION: main
11-30 18:22:50.085: E/AndroidRuntime(13114): java.lang.NullPointerException
11-30 18:22:50.085: E/AndroidRuntime(13114): at com.dr.droid.lee.HospResult$GeocoderTask.onPostExecute(HospResult.java:130)
11-30 18:22:50.085: E/AndroidRuntime(13114): at com.dr.droid.lee.HospResult$GeocoderTask.onPostExecute(HospResult.java:1)
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask.finish(AsyncTask.java)
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask.access$600(AsyncTask.java)
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
这是我的班级代码
public class HospResult extends MapActivity {
String hcity;
String hregion;
String hadd;
String hname;
String hcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Typeface type = Typeface.createFromAsset(this.getAssets(),"MyriadPro-Bold.ttf");
setContentView(R.layout.map);
TextView hosname = (TextView) findViewById(R.id.tvName);
TextView hosreg = (TextView) findViewById(R.id.tvRegion);
TextView hosadd = (TextView) findViewById(R.id.tvAddress);
TextView hoscon = (TextView) findViewById(R.id.tvContact);
TextView hoscity = (TextView) findViewById(R.id.tvCity);
Bundle receive = getIntent().getExtras();
hcity = receive.getString("city");
hregion = receive.getString("region");
hadd = receive.getString("address");
hname = receive.getString("name");
hcon = receive.getString("contact");
hosname.setText(hname);
hosreg.setText(hregion);
hoscity.setText(hcity);
hosadd.setText(hcon);
hoscon.setText(hadd);
hosname.setTypeface(type);
hosreg.setTypeface(type);
hosadd.setTypeface(type);
hoscity.setTypeface(type);
hoscon.setTypeface(type);
//MapView mv = (MapView) findViewById(R.id.mv1);
//MapController mp = mv.getController();
//mp.setZoom(15);
String loc = hosname.getText().toString();
//Toast.makeText(this, loc, Toast.LENGTH_SHORT).show();
if(loc!=null && !loc.equals("")){
new GeocoderTask().execute(loc);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 1);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
// Getting Reference to MapView of the layout activity_main
MapView mapView = (MapView) findViewById(R.id.mv1);
// Setting ZoomControls
mapView.setBuiltInZoomControls(true);
// Getting MapController for the MapView
MapController mc = mapView.getController();
mc.setZoom(18);
// Getting Drawable object corresponding to a resource image
Drawable drawable = getResources().getDrawable(R.drawable.marker);
// Getting Overlays of the map
List<Overlay> overlays = mapView.getOverlays();
// Creating an ItemizedOverlay
LocationOverlay locationOverlay = new LocationOverlay(drawable,getBaseContext());
// Clearing the overlays
overlays.clear();
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
// Redraws the map to clear the overlays
mapView.invalidate();
}
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
GeoPoint p = new GeoPoint(
(int)(address.getLatitude()*1E6),
(int)(address.getLongitude()*1E6)
);
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
// Creating an OverlayItem to mark the point
OverlayItem overlayItem = new OverlayItem(p, "Location",addressText);
// Adding the OverlayItem in the LocationOverlay
locationOverlay.addOverlay(overlayItem);
// Adding locationOverlay to the overlay
overlays.add(locationOverlay);
// Locate the first location
if(i==0)
mc.animateTo(p);
}
// Redraws the map
mapView.invalidate();
}
}
}
public类HospResult扩展了MapActivity{
字符串同构性;
弦区;
字符串hadd;
我;
字符串hcon;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
Typeface type=Typeface.createFromAsset(this.getAssets(),“MyriadPro Bold.ttf”);
setContentView(R.layout.map);
TextView hosname=(TextView)findViewById(R.id.tvName);
TextView hosreg=(TextView)findViewById(R.id.tvRegion);
TextView hosadd=(TextView)findViewById(R.id.tvAddress);
TextView hoscon=(TextView)findViewById(R.id.tvContact);
TextView hoscity=(TextView)findViewById(R.id.tvCity);
Bundle receive=getIntent().getExtras();
hcity=receive.getString(“城市”);
hregion=receive.getString(“region”);
hadd=receive.getString(“地址”);
hname=receive.getString(“名称”);
hcon=receive.getString(“联系人”);
hosname.setText(hname);
hosreg.setText(赫雷吉翁);
hoscity.setText(hcity);
hosadd.setText(hcon);
hoscon.setText(hadd);
hosname.setTypeface(类型);
hosreg.setTypeface(类型);
hosadd.setTypeface(类型);
hoscity.setTypeface(类型);
hoscon.setTypeface(类型);
//MapView mv=(MapView)findViewById(R.id.mv1);
//MapController mp=mv.getController();
//议员setZoom(15);
字符串loc=hosname.getText().toString();
//Toast.makeText(this,loc,Toast.LENGTH_SHORT).show();
如果(loc!=null&!loc.equals(“”){
新的GeocoderTask().execute(loc);
}
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
//用于访问地理编码Web服务的AsyncTask类
私有类GeocoderTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…位置名称){
//创建Geocoder类的实例
Geocoder Geocoder=新的Geocoder(getBaseContext());
列表地址=空;
试一试{
//获取最多3个与输入文本匹配的地址
addresses=geocoder.getFromLocationName(locationName[0],1);
}捕获(IOE异常){
e、 printStackTrace();
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表地址){
//获取对布局活动\u main的MapView的引用
MapView MapView=(MapView)findViewById(R.id.mv1);
//设置缩放控件
mapView.SetBuilTinZoomControl(真);
//获取MapView的MapController
MapController mc=mapView.getController();
mc.setZoom(18);
//获取与资源映像对应的可绘制对象
Drawable Drawable=getResources().getDrawable(R.Drawable.marker);
//获取地图的覆盖图
List overlays=mapView.getOverlays();
//创建ItemizedOverlay
LocationOverlay LocationOverlay=新的LocationOverlay(可绘制,getBaseContext());
//清除覆盖物
覆盖。清除();
if(addresses==null | | addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到位置”,Toast.LENGTH_SHORT.show();
//重新绘制地图以清除覆盖
mapView.invalidate();
}
//在谷歌地图上为每个匹配地址添加标记
对于(int i=0;i 0?地址。getAddressLine(0):“”,
address.getCountryName());
//创建覆盖项以标记点
OverlayItem OverlayItem=新的OverlayItem(p,“位置”,地址文本);
//在LocationOverlay中添加OverlayItem
locationOverlay.addOverlay(overlayItem);
//将locationOverlay添加到覆盖中
叠加。添加(locationOverlay);
//找到第一个位置
如果(i==0)
司马迁(p),;
}
//重新绘制地图
mapView.invalidate();
}
}
}
第130行从这里开始
for(int i=0;i<addresses.size();i++){
for(int i=0;i尝试
使用列表地址=新列表();
正如@Carnal在这里非常正确地说的那样,如果您确信您是通过方法geocoder获取列表的,那么您不必初始化列表。getFromLocationName
然后您可以使用List addresses=null
,或者您可以检查列表的大小,并根据您在for循环中使用它们的情况使用它们。您不需要初始化List addresses=new List();
,由于geocoder.getFromLocationName
返回一个List
,您仍然可以拥有List addresses=null
。问题似乎是在执行循环之前没有检查地址列表是否为null。这似乎会使程序崩溃
执行此检查:
if(addresses != null && addresses.size() > 0){
for(int i=0;i<addresses.size();i++){
// rest of your code..
}
}
if(addresses!=null&&addresses.size()>0){
对于(int i=0;iI当我在新列表下添加那行代码时,我收到了一个错误。红线()在我从电脑上拔下设备后,它工作正常。感谢您的回复,先生!是的,但是