子类中的地理编码器上下文

时间:2010-12-08 17:41:22

标签: android geocoding android-context

我的主要活动是AddressFinder,在这里我开始AddressController

AddressController ac = new AddressController();

在某些情况下,AddressController应该更新:

import android.content.Context;
....
private void updateAddresses() throws IOException {
    Geocoder geocoder = new Geocoder(context);
 for (Address a: address) {
 List<Address> addressIn = geocoder.getFromLocation(a.getLatitude(), 
                                                       a.getLongitude(), 1);
 }
}

现在我不知道我必须使用哪种上下文。我不明白如何使用它。我尝试了thiscontextgetBaseContext()getApplicationContext(),但没有任何效果。此外,我试图给Adresscontroller一个主要活动的contextgetApplicationContext)参数。

2 个答案:

答案 0 :(得分:1)

我想我有一个解决方案。在主要活动中,我使用以下内容:

ac.updateAddr(getApplicationContext());

在AddressController中,我改变了:

public void updateAddr(Context c) throws IOException {
    updateAddresses(c);
}

这就是我向Geocoder中的AddressController提供上下文的方式。工作正常。

答案 1 :(得分:0)

Activity班级中创建变量:

Context con = this;  
... 
Geocoder geocoder = new Geocoder(con);

也许使用其他构造函数?:

public Geocoder (Context context, Locale locale)
相关问题