【发布时间】:2015-04-11 18:34:16
【问题描述】:
我有一个应用程序,我从使用AsyncTask 的地址生成经度和纬度,我从doInBackground 开始生成坐标我的问题是,在我的代码中,当我输入这一行时,Handler 似乎没有打开:
viedotGeoCoo();
在AsyncTask 之外,似乎一切都运行良好。
这是我的代码:
public class AddEvent extends Activity {
Button addressButton, timeButton;
TextView addressTV,textView2;
TextView latLongTV, longCo, textView4;
EditText editNosaukums;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_event);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
addressTV = (TextView) findViewById(R.id.addressTV);
latLongTV = (TextView) findViewById(R.id.latLongTV);
longCo = (TextView) findViewById(R.id.longCo);
textView4 = (TextView) findViewById(R.id.textView4);
editNosaukums = (EditText) findViewById(R.id.editNosaukums);
textView2 = (TextView) findViewById(R.id.textView2);
addressButton = (Button) findViewById(R.id.addressButton);
addressButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.addressButton:
DownloadFilesTask task = new DownloadFilesTask();
task.execute((Void[]) null);
break;
}
}
});
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... urls) {
Looper.prepare();
viedotGeoCoo();
return null;
}
protected void onProgressUpdate(Void... progress) {
}
protected void onPostExecute(Void result) {
Log.e("Add_Event", "GEO IZDEVAAS");
sutitDatus();
}
}
public void viedotGeoCoo() {
EditText editValsts = (EditText) findViewById(R.id.editValsts);
EditText editPilseta = (EditText) findViewById(R.id.editPilseta);
EditText editIelaNr = (EditText) findViewById(R.id.editIelaNr);
String valsts = editValsts.getText().toString();
String pilseta = editPilseta.getText().toString();
String ielanr = editIelaNr.getText().toString();
String address = valsts + " "+ pilseta + " " + ielanr;
Log.e("ADD_EVENT", "HANDLER SAAKAS");
GeocodingLocation locationAddress = new GeocodingLocation();
locationAddress.getAddressFromLocation(address,
getApplicationContext(), new GeocoderHandler());//jadublice jaataisa speciala klase
GeocodingLocationLat locationAddressLat = new GeocodingLocationLat();
locationAddressLat.getAddressFromLocation(address,
getApplicationContext(), new GeocoderHandlerLat());//jadublice jaataisa speciala klase
Log.e("ADD_EVENT", "GEO GENEREETS");
}
//sanjem stringu no com.wunderlist.slidinglayersample.GeocodingLocation.java
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
Log.e("Add_Event", "HANDLER_LONG");
}
}
private class GeocoderHandlerLat extends Handler {
@Override
public void handleMessage(Message message) {
Log.e("Add_Event", "Handler_Lat");
}
}
}
有人知道为什么我的代码不能正常工作吗?
【问题讨论】:
-
只需将所有扩展视图并检索其文本值的代码移动到 UI 线程并将“地址”作为参数传递给 AsyncTask - 这将使您的代码更具可读性并解决您的问题.
-
@Egor no 那没有用
标签: android android-asynctask handler