Java 如何在活动中显示asynctask值
我不想使用async tack在活动中显示值。异步任务的代码是:Java 如何在活动中显示asynctask值,java,android,android-asynctask,Java,Android,Android Asynctask,我不想使用async tack在活动中显示值。异步任务的代码是: private class BackgroundGetSignal extends AsyncTask<String, Void, Void> { private TextView name; //private TextView latitudeField; //private TextView longitudeField; //private TextView placeName;
private class BackgroundGetSignal extends AsyncTask<String, Void, Void> {
private TextView name;
//private TextView latitudeField;
//private TextView longitudeField;
//private TextView placeName;
Double lat;
Double longit;
String addre;
MyService myService;
private Intent i;
private Context context;
public BackgroundGetSignal(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String[] o) {
myService = new MyService();
if (myService.canGetLocation()) {
lat = myService.getLatitude();
longit = myService.getLongitude();
addre = myService.getAddress();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
latitudeField = (TextView) findViewById(R.id.txtLatitude);
longitudeField = (TextView) findViewById(R.id.txtLongitude);
placeName = (TextView) findViewById(R.id.txtLocation);
latitudeField.setText(lat.toString());
longitudeField.setText(longit.toString());
placeName.setText(addre);
}
}
但应用程序正在崩溃。请有人告诉我我做错了什么。最好的方法是在onPostExecute()方法中提供try-catch,并将ui更新代码移动到try块。因为在onPostExecute()执行时,如果活动不在前台,则有可能发生异常。如果发布崩溃日志,将有助于对问题进行详细分析。为isuue添加错误日志。发布的代码不正确:文本视图被注释掉。给您一个如何在AsyncTask中良好运行的提示:使用回调接口在
onPostExecuted
中传递计算值,并在AsyncTask执行类中实现这些接口以获取传递的值。您遇到了什么异常?
new BackgroundGetSignal(this).execute();