1

我试图将 GeoPoint 对象从其 Lat 和 Long 值反转为实际的字符串地址。

到目前为止,我将 GeoPoint 对象传递给一个方法,提取 lat 和 long 值,将地址存储在一个数组中,将第一个地址设置为“添加”字符串变量,并通过警报对话框重新设置。 .

这是显示 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;  

        }

日志猫:

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)
4

1 回答 1

0

地址 = 地址.get(0).getAddressLine(0); 城市 = 地址.get(0).getAddressLine(1); 国家=addresses.get(0).getAddressLine(2);

如果上面有一些是空的,你会得到空指针异常,因为你没有在 if 块中使用 try catch,

使用下面的代码,

 for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
     {
          try{  
              add += addresses.get(0).getAddressLine(i) + "\n";
          }
          catch(Exception e)
          {}
     }
于 2013-03-11T15:31:24.827 回答