我想要获得这个城市,但我尝试使用以下代码并向我显示“构造函数Geocoder(SitesAdapter,Locale)未定义”,我在这个论坛和其他人中尝试了很多方法而且找不到合适的解决方案。 Geocoder(Context,Locale)中正确的上下文是什么?。
地址解析器:
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);
完整代码:
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);
...
}
}
更新 最终守则 - 第一个解决方案:
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 - 第一个解决方案:
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
答案 0 :(得分:1)
我可以解决该代码的问题:
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();
}
您可以查看sample。祝你好运
答案 1 :(得分:0)
您必须使用Context
作为第一个参数(可能是Activity
)。你有一个传递适配器的构造函数。只需将其添加到局部变量并将其用于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);
...
}
}