我为AsyncTask创建了一个新文件,因此我可以将我的代码分解为MVP。
文件: TranslateAddress.java
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);
}
}
}
这些是有错误的行:
final Dialog customDialog = new Dialog(MainActivity.this);
错误: MainActivity不是封闭类
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
错误:与上述相同,MainActivity不是封闭类
MainActivity.OpenFragment("SolicitationFramgnet");
错误:非静态方法无法从静态上下文引用OpenFragment。 我可以通过使OpenFragment静态修复此错误,但是,它会破坏其中一半的代码。
EditText localText = (EditText) findViewById(R.id.localText);
错误:无法解析方法“findViewById(int)”
答案 0 :(得分:1)
将您的AsyncTask
移动到另一个.java
文件,您将丢失MainActivity
的引用,因为在它作为InnerClass运行之前它现在只是一个Java类,因此您无法使用MainActivity这种情况下的方法或静态上下文。对findViewById
方法来说是一回事,因为它属于您的Activity
,而不属于您的AsyncTask
。要修复它,您必须在构造函数和主布局中传递上下文,以便您可以使用此视图(作为构造函数的参数)在FindViewById
文件中使用TranslateAddress
方法。
这样的事情:
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);
}
}
}
答案 1 :(得分:0)
对于上下文,请从MainActivity传递它,如下所示:
在AsyncTask中,创建一个构造函数
Context context;
TranslateAddress(Context c){
context = c;
}
现在使用context
代替MainActivity.this
。并从MainActivity执行AsyncTask,如下所示:
new TranslateAddress(this).execute();
和findViewById()
,请使用以下代码:
context.findViewById("YOUR_ID");