Error: The constructor Geocoder (Context, Locale)

2019-08-12 04:48发布

问题:

I want to get the city, but I try with the following code and show me "The constructor Geocoder(SitesAdapter, Locale) is undefined", I try many ways in this forum and anothers and I'not find the right solution. What would be the right context in Geocoder(Context, Locale)?.

Geocoder:

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(SitesAdapter.this, Locale.getDefault());
addresses = geocoder.getFromLocation(lat, lng, 1);      
String city= addresses.get(0).getAddressLine(1);

Full code:

public class SitesAdapter extends ArrayAdapter<StackSite> {

    public SitesAdapter(Context ctx, int textViewResourceId, List<StackSite> sites) {
        super(ctx, textViewResourceId, sites);
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        ...         
        //Get our View References
        TextView tituloTxt = (TextView)row.findViewById(R.id.tituloTxt);
        TextView fechaTxt = (TextView)row.findViewById(R.id.fechaTxt);
        TextView magTxt = (TextView)row.findViewById(R.id.magTxt);
        TextView depthTxt = (TextView)row.findViewById(R.id.depthTxt);

        String stringlat = this.getItem(pos).getLat();
        Double lat = Double.parseDouble(stringlat); 
        String stringlng = this.getItem(pos).getLng();
        Double lng = Double.parseDouble(stringlng);

        String dateUTC = this.getItem(pos).getFecha();
        String stringDepth = this.getItem(pos).getDepth();
        Double depth = Double.parseDouble(stringDepth);             

        //GET CITY
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(SitesAdapter.this, Locale.getDefault());
        addresses = geocoder.getFromLocation(lat, lng, 1);      
        String city= addresses.get(0).getAddressLine(1);    
        ...                                 
    }    
}

UPDATE Final Code - first solution:

Geocoder geocoder;
List<Address> addresses = null;
    geocoder = new Geocoder(mContext, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(lat, lng, 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }      
    String city= addresses.get(0).getAddressLine(1);

LOGCAT - first solution:

11-07 17:27:32.283: I/StackSites(3808): adapter size = 327
11-07 17:27:32.303: I/StackSites(3808): getView pos = 0
11-07 17:27:32.383: W/System.err(3808): java.io.IOException: Service not Available
11-07 17:27:32.413: W/System.err(3808):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
11-07 17:27:32.413: W/System.err(3808):     at com.gestionderiesgos.sismosecuador.SitesAdapter.getView(SitesAdapter.java:101)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.AbsListView.obtainView(AbsListView.java:2271)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.ListView.makeAndAddView(ListView.java:1812)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.ListView.fillDown(ListView.java:687)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.ListView.fillDown(ListView.java:666)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.ListView.fillFromTop(ListView.java:758)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.ListView.layoutChildren(ListView.java:1665)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.AbsListView.onLayout(AbsListView.java:2106)
11-07 17:27:32.413: W/System.err(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.413: W/System.err(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:953)
11-07 17:27:32.413: W/System.err(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.413: W/System.err(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.413: W/System.err(3808):     at android.widget.FrameLayout.onLayout(FrameLayout.java:461)
11-07 17:27:32.413: W/System.err(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.413: W/System.err(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.423: W/System.err(3808):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1688)
11-07 17:27:32.423: W/System.err(3808):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1546)
11-07 17:27:32.423: W/System.err(3808):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1451)
11-07 17:27:32.423: W/System.err(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.423: W/System.err(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.433: W/System.err(3808):     at android.widget.FrameLayout.onLayout(FrameLayout.java:461)
11-07 17:27:32.433: W/System.err(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.433: W/System.err(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.433: W/System.err(3808):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1872)
11-07 17:27:32.433: W/System.err(3808):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1693)
11-07 17:27:32.433: W/System.err(3808):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-07 17:27:32.443: W/System.err(3808):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4218)
11-07 17:27:32.443: W/System.err(3808):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-07 17:27:32.443: W/System.err(3808):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-07 17:27:32.443: W/System.err(3808):     at android.view.Choreographer.doFrame(Choreographer.java:525)
11-07 17:27:32.443: W/System.err(3808):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-07 17:27:32.443: W/System.err(3808):     at android.os.Handler.handleCallback(Handler.java:615)
11-07 17:27:32.443: W/System.err(3808):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 17:27:32.443: W/System.err(3808):     at android.os.Looper.loop(Looper.java:137)
11-07 17:27:32.443: W/System.err(3808):     at android.app.ActivityThread.main(ActivityThread.java:4744)
11-07 17:27:32.443: W/System.err(3808):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 17:27:32.443: W/System.err(3808):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 17:27:32.453: W/System.err(3808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 17:27:32.453: W/System.err(3808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 17:27:32.453: W/System.err(3808):     at dalvik.system.NativeStart.main(Native Method)
11-07 17:27:32.453: D/AndroidRuntime(3808): Shutting down VM
11-07 17:27:32.453: W/dalvikvm(3808): threadid=1: thread exiting with uncaught exception (group=0x41959318)
11-07 17:27:32.463: E/AndroidRuntime(3808): FATAL EXCEPTION: main
11-07 17:27:32.463: E/AndroidRuntime(3808): java.lang.NullPointerException
11-07 17:27:32.463: E/AndroidRuntime(3808):     at com.gestionderiesgos.sismosecuador.SitesAdapter.getView(SitesAdapter.java:106)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.AbsListView.obtainView(AbsListView.java:2271)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.ListView.makeAndAddView(ListView.java:1812)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.ListView.fillDown(ListView.java:687)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.ListView.fillDown(ListView.java:666)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.ListView.fillFromTop(ListView.java:758)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.ListView.layoutChildren(ListView.java:1665)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.AbsListView.onLayout(AbsListView.java:2106)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:953)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.FrameLayout.onLayout(FrameLayout.java:461)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1688)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1546)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1451)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.widget.FrameLayout.onLayout(FrameLayout.java:461)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.View.layout(View.java:13879)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewGroup.layout(ViewGroup.java:4472)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1872)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1693)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4218)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.Choreographer.doFrame(Choreographer.java:525)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.os.Handler.handleCallback(Handler.java:615)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.os.Looper.loop(Looper.java:137)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at android.app.ActivityThread.main(ActivityThread.java:4744)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 17:27:32.463: E/AndroidRuntime(3808):     at dalvik.system.NativeStart.main(Native Method)
11-07 17:27:34.875: I/Process(3808): Sending signal. PID: 3808 SIG: 9

回答1:

I can to resolve problem with that code:

String city = null;
Geocoder gc = new Geocoder(mContext, Locale.getDefault());      
List<Address> list;
try {
    list = gc.getFromLocation(lat, lng,1);
    if (list != null && list.size() > 0) {
        Address address = list.get(0);
        city = address.getAdminArea();
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

You can check this sample . Good luck



回答2:

You have to use a Context as first parameter (could be an Activity). You have one passed on the constructor of your adapter. Just add it to a local variable and use it for the Geocoder.

public class SitesAdapter extends ArrayAdapter<StackSite> {
    private Context mContext;

    public SitesAdapter(Context ctx, int textViewResourceId, List<StackSite> sites) {
        super(ctx, textViewResourceId, sites);
        mContext = ctx;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        ...
        //GET CITY
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(mContext, Locale.getDefault());
        addresses = geocoder.getFromLocation(lat, lng, 1);      
        String city= addresses.get(0).getAddressLine(1);        

        ...       
    }
}