我试图将GeoPoint对象从其Lat和Long值反转为实际的字符串地址。
到目前为止,我将GeoPoint对象传递给一个方法,提取lat和long值,将地址存储在一个数组中,将第一个地址设置为'add'字符串变量并重新设置为使用警报对话..
这是显示GeoPoint'p'被发送以及如何调用该方法:
String address = convertGpToLoc(point);
city.setText("Address: " + address + "");
CityClickListner类:
class CityClickListener extends Activity implements OnClickListener {
private GeoName geoName = null;
CityClickListener(GeoName name) {
this.geoName = name;
}
@Override
public void onClick(View v) {
Double Lattt = geoName.getGeometry().getLocation().getLat();
Double Longgg = geoName.getGeometry().getLocation().getLng();
updateMap(Lattt, Longgg);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setView(createView());
builder.setTitle("Details of " + geoName.getName());
builder.setCancelable(true);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
convertGpToLoc方法:
public String convertGpToLoc(GeoPoint p)
{
String add = "";
try {
Geocoder gc = new Geocoder(CityClickListener.this, Locale.ENGLISH);
List<Address> addresses = gc.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
if (addresses.size() < 0)
{
return null;
}
else
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
{
add += addresses.get(0).getAddressLine(i) + "\n";
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return add;
}
logcat的:
03-11 15:16:10.952: E/AndroidRuntime(304): FATAL EXCEPTION: main
03-11 15:16:10.952: E/AndroidRuntime(304): java.lang.NullPointerException
03-11 15:16:10.952: E/AndroidRuntime(304): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.location.Geocoder.<init>(Geocoder.java:64)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.convertGpToLoc(PostalCodeAdapter.java:207)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.createView(PostalCodeAdapter.java:175)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.onClick(PostalCodeAdapter.java:1 14)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.view.View.performClick(View.java:2408)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.view.View$PerformClick.run(View.java:8816)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Handler.handleCallback(Handler.java:587)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Looper.loop(Looper.java:123)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 15:16:10.952: E/AndroidRuntime(304): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 15:16:10.952: E/AndroidRuntime(304): at java.lang.reflect.Method.invoke(Method.java:521)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-11 15:16:10.952: E/AndroidRuntime(304): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
address = addresses.get(0).getAddressLine(0); city = addresses.get(0).getAddressLine(1); country = addresses.get(0).getAddressLine(2);
如果其中一些为null,则会得到nullpointer异常,因为你没有在if块中使用try catch,
使用以下代码
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
{
try{
add += addresses.get(0).getAddressLine(i) + "\n";
}
catch(Exception e)
{}
}