我有 2 个活动:MainActivity与main.xml布局相关联(应用程序“主”屏幕)和AboutActivity与about.xml布局相关联(应用程序“关于”屏幕)。
虽然在AboutActivity,里面的一个Async任务MainActivity仍然试图访问main.xml。结果我的应用程序停止工作。
我有什么办法吗?
- 当
Async用户MainActivity从AboutActivity - 或仍然
main.xml在后台访问AboutActivity
附加信息:
MainActivity是启动活动。AboutActivity extends MainActivity. 用户可以转到“关于”屏幕/切换到AboutActivity使用选项菜单。
其中的Async任务MainActivity将用户的当前位置放入文本视图中。about.xml仅包含静态文本。AboutActivity除了显示之外什么都不做about.xml。
关于活动:
public class AboutActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating a new non-ui thread task to download Google place json data
PlacesTask placesTask = new PlacesTask();
// Invokes the "doInBackground()" method of the class PlaceTask
placesTask.execute(sb.toString());
}
/** A class, to download Google Places */
private class PlacesTask extends AsyncTask<String, Integer, String>{
String data = null;
// Invoked by execute() method of this object
@Override
protected String doInBackground(String... url) {
//make asynctask wait for debugger
//android.os.Debug.waitForDebugger();
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d(DEBUG,e.toString());
}
return data;
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String result){
TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
curLoc.setText(result);
}
}
}