Android 将异步任务移动到新文件
我为AsyncTask创建了一个新文件,以便将代码分解为MVP 文件:TranslateAddress.javaAndroid 将异步任务移动到新文件,android,Android,我为AsyncTask创建了一个新文件,以便将代码分解为MVP 文件:TranslateAddress.java public class TranslateAddress extends AsyncTask<String, Void, String> { final Dialog customDialog = new Dialog(MainActivity.this); protected void onPreExecute() { super.onPreExecut
public class TranslateAddress extends AsyncTask<String, Void, String> {
final Dialog customDialog = new Dialog(MainActivity.this);
protected void onPreExecute() {
super.onPreExecute();
customDialog.setContentView(R.layout.custom_location_dialog);
customDialog.setTitle("Looking for address");
TextView text = (TextView) customDialog.findViewById(R.id.textView);
text.setText("Looking for address");
customDialog.show();
}
protected String doInBackground(String... params) {
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
if (geocoder != null) {
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
// do download here
} else {
Log.e("Error", "Geocoder returned Null");
MainActivity.OpenFragment("SolicitationFramgnet");
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
//dialog.dismiss();
customDialog.dismiss();
EditText localText = (EditText) findViewById(R.id.localText);
if (localText != null) {
localText.setText(address);
}
}
}
公共类TranslateAddress扩展异步任务{
最终对话框customDialog=新建对话框(MainActivity.this);
受保护的void onPreExecute(){
super.onPreExecute();
customDialog.setContentView(R.layout.custom\u location\u对话框);
customDialog.setTitle(“查找地址”);
TextView text=(TextView)customDialog.findViewById(R.id.TextView);
text.setText(“查找地址”);
customDialog.show();
}
受保护的字符串doInBackground(字符串…参数){
地理编码器;
列表地址=空;
geocoder=新的geocoder(MainActivity.this,Locale.getDefault());
如果(地理编码器!=null){
试一试{
addresses=geocoder.getFromLocation(纬度,经度,1);//这里1表示返回的最大位置结果,由它建议的1到5个文档表示
}捕获(IOE异常){
e、 printStackTrace();
}
address=addresses.get(0).getAddressLine(0);//如果存在除此之外的任何其他地址行,请通过getMaxAddressLineIndex()检查最大可用地址行
city=addresses.get(0.getLocation();
state=addresses.get(0.getAdminArea();
country=addresses.get(0.getCountryName();
postalCode=addresses.get(0.getPostalCode();
knownName=addresses.get(0).getFeatureName();//仅当可用时,否则返回NULL
//请在这里下载
}否则{
Log.e(“错误”,“地理编码器返回空”);
MainActivity.OpenFragment(“征求FramNet”);
}
返回null;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
//dialog.dismise();
customDialog.disclose();
EditText localText=(EditText)findViewById(R.id.localText);
if(localText!=null){
localText.setText(地址);
}
}
}
以下是有错误的行:
final Dialog customDialog=新建对话框(MainActivity.this)代码>
geocoder=newgeocoder(MainActivity.this,Locale.getDefault())代码>
MainActivity.OpenFragment(“requisitionframgnet”)代码>
EditText localText=(EditText)findviewbyd(R.id.localText)代码>
错误:无法解析方法“findViewById(int)”将
AsyncTask
移动到另一个.java
文件会丢失对MainActivity
的引用,因为以前它是一个内部类,现在它只是一个java类,因此,在这种情况下不能使用MainActivity方法或静态上下文。对于findViewById
方法也是如此,因为它属于活动
而不是异步任务
。要修复它,您必须在构造函数和主布局中传递上下文,这样您就可以使用此视图(作为构造函数的参数)在TranslateAddress
文件中使用FindViewById
方法
大概是这样的:
public class TranslateAddress extends AsyncTask<String, Void, String> {
final Dialog customDialog;
private MainActivity mainActivity;
private View view;
public TranslateAddress(View view, MainActivity mainActivity){
this.view = view;
this.mainActivity = mainActivity;
}
protected void onPreExecute() {
super.onPreExecute();
customDialog = new Dialog(mainActivity);
customDialog.setContentView(R.layout.custom_location_dialog);
customDialog.setTitle("Looking for address");
TextView text = (TextView) customDialog.findViewById(R.id.textView);
text.setText("Looking for address");
customDialog.show();
}
protected String doInBackground(String... params) {
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(mainActivity, Locale.getDefault());
if (geocoder != null) {
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
// do download here
} else {
Log.e("Error", "Geocoder returned Null");
mainActivity.OpenFragment("SolicitationFramgnet");
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
//dialog.dismiss();
customDialog.dismiss();
EditText localText = (EditText) view.findViewById(R.id.localText);
if (localText != null) {
localText.setText(address);
}
}
}
公共类TranslateAddress扩展异步任务{
最后的对话;
私人活动;
私人视野;
公共TranslateAddress(视图,主活动主活动){
this.view=视图;
this.mainActivity=mainActivity;
}
受保护的void onPreExecute(){
super.onPreExecute();
customDialog=新建对话框(mainActivity);
customDialog.setContentView(R.layout.custom\u location\u对话框);
customDialog.setTitle(“查找地址”);
TextView text=(TextView)customDialog.findViewById(R.id.TextView);
text.setText(“查找地址”);
customDialog.show();
}
受保护的字符串doInBackground(字符串…参数){
地理编码器;
列表地址=空;
geocoder=新的geocoder(mainActivity,Locale.getDefault());
如果(地理编码器!=null){
试一试{
addresses=geocoder.getFromLocation(纬度,经度,1);//这里1表示返回的最大位置结果,由它建议的1到5个文档表示
}捕获(IOE异常){
e、 printStackTrace();
}
address=addresses.get(0).getAddressLine(0);//如果存在除此之外的任何其他地址行,请通过getMaxAddressLineIndex()检查最大可用地址行
city=addresses.get(0.getLocation();
state=addresses.get(0.getAdminArea();
country=addresses.get(0.getCountryName();
postalCode=addresses.get(0.getPostalCode();
knownName=addresses.get(0).getFeatureName();//仅当可用时,否则返回NULL
//请在这里下载
}否则{
Log.e(“错误”,“地理编码器返回空”);
mainActivity.OpenFragment(“征求FramNet”);
}
返回null;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
//dialog.dismise();
customDialog.disclose();
EditText localText=(EditText)view.findViewById(R.id.localText);
if(localText!=null){
localText.setText(地址);
}
}
}
对于上下文,请按如下方式从MainActivity传递它:
public class TranslateAddress extends AsyncTask<String, Void, String> {
final Dialog customDialog;
private MainActivity mainActivity;
private View view;
public TranslateAddress(View view, MainActivity mainActivity){
this.view = view;
this.mainActivity = mainActivity;
}
protected void onPreExecute() {
super.onPreExecute();
customDialog = new Dialog(mainActivity);
customDialog.setContentView(R.layout.custom_location_dialog);
customDialog.setTitle("Looking for address");
TextView text = (TextView) customDialog.findViewById(R.id.textView);
text.setText("Looking for address");
customDialog.show();
}
protected String doInBackground(String... params) {
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(mainActivity, Locale.getDefault());
if (geocoder != null) {
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
// do download here
} else {
Log.e("Error", "Geocoder returned Null");
mainActivity.OpenFragment("SolicitationFramgnet");
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
//dialog.dismiss();
customDialog.dismiss();
EditText localText = (EditText) view.findViewById(R.id.localText);
if (localText != null) {
localText.setText(address);
}
}
}
在AsyncTask中,创建一个构造函数
Context context;
TranslateAddress(Context c){
context = c;
}
现在使用context
而不是MainActivity。这是。并从MainActivity执行AsyncTask,如下所示:
new TranslateAddress(this).execute();
对于findViewById()
,请使用以下代码:
context.findViewById("YOUR_ID");
谢谢你的意见,两件事。1) customDialog没有初始化,所以我设置了this.customDialog=newDialog(上下文)
方法内部公共TranslateAddress(视图、上下文)
。2)