Mic*_*ael 19 java android android-context android-asynctask
所以我正在使用Android和AsyncTask类开发我的第一个多线程应用程序.我正在尝试使用它在第二个线程中触发Geocoder,然后使用onPostExecute更新UI,但我仍然遇到了正确的Context问题.
我在主线程上使用Contexts有点蠢蠢欲动,但我不确定Context是什么或如何在后台线程上使用它,我还没有找到任何好的例子.有帮助吗?以下是我要做的事情的摘录:
public class GeoCode extends AsyncTask<GeoThread, Void, GeoThread> {
@Override
protected GeoThread doInBackground(GeoThread... i) {
List<Address> addresses = null;
Geocoder geoCode = null;
geoCode = new Geocoder(null); //Expects at minimum Geocoder(Context context);
addresses = geoCode.getFromLocation(GoldenHour.lat, GoldenHour.lng, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
由于上下文不正确,它在第六行仍然失败.
Rea*_*oid 18
@Eugene van der Merwe
以下代码对我有用:) - >
public class ApplicationLauncher extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.applicationlauncher);
LoadApplication loadApplication = new LoadApplication(this);
loadApplication.execute(null);
}
private class LoadApplication extends AsyncTask {
Context context;
ProgressDialog waitSpinner;
ConfigurationContainer configuration = ConfigurationContainer.getInstance();
public LoadApplication(Context context) {
this.context = context;
waitSpinner = new ProgressDialog(this.context);
}
@Override
protected Object doInBackground(Object... args) {
publishProgress(null);
//Parsing some stuff - not relevant
configuration.initialize(context);
return null;
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
// Only purpose of this method is to show our wait spinner, we dont
// (and can't) show detailed progress updates
waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true);
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
waitSpinner.cancel();
}
}
}
Run Code Online (Sandbox Code Playgroud)
干杯,
Ready4Android
我做了更多研究,有人建议将其传递给线程(不知道为什么我没有想到这一点)。我通过参数将其传递给地理编码器线程,就像这样,它起作用了。
| 归档时间: |
|
| 查看次数: |
25421 次 |
| 最近记录: |