0
我有在取消異步任務問題或可我在錯誤的方式取消它,我有一個主要活動,其中有兩個活動的一,二,問題是,的Android異步任務取消錯誤
我想在一個活動中取消異步任務,這樣當活動二開始時,它可以再次啓動或執行,應用程序不會崩潰。
OneActivity
public class OneActivity extends AppCompatActivity {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
private RemoteDataTask tsk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.gpsicon);
AsyncTask tsk = new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(OneActivity.this);
mProgressDialog.setMessage("Please wait ......");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"One");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (com.parse.ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView) findViewById(R.id.one_list);
adapter = new ArrayAdapter<String>(OneActivity.this,
R.layout.one_row_item);
for (ParseObject one : ob) {
adapter.add((String) one.get("onec"));
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
tsk.cancel(true);
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
tsk.cancel(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
TwoActivity
public class TwoActivity extends AppCompatActivity {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
private RemoteDataTask tsk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.gpsicon);
AsyncTask tsk = new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(TwoActivity.this);
mProgressDialog.setMessage("Please wait ......");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Two");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (com.parse.ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView) findViewById(R.id.two_list);
adapter = new ArrayAdapter<String>(TwoActivity.this,
R.layout.two_row_item);
for (ParseObject two : ob) {
adapter.add((String) two.get("twoc"));
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
tsk.cancel(true);
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
tsk.cancel(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
在此先感謝我只想簡單知道如何取消異步任務的工作完成之後。
問題來了,當我從oneactivity回來,然後打開兩個活動,然後應用程序崩潰,當我再次打開該應用程序,然後兩個活動輕鬆打開幫助我 –
發佈您的例外。因爲我懷疑你在訪問AsyncTask中的Activity引用時遇到了問題。 – user370305