我有這個活動。內部活動我有3個asynctask,我需要其中的一個在onCreate方法中執行。我想按照這個網站上的教程http://www.androidhive.info/2013/11/android-working-with-action-bar/(幾乎在最後)實現一個刷新按鈕,但問題是我想要操作欄刷新按鈕來執行asyncTask,它首先在onCreate方法中調用。例如活動:何時執行asynctask
package info.androidhive.actionbar;
import info.androidhive.actionbar.model.SpinnerNavItem;
import info.androidhive.info.actionbar.adapter.TitleNavigationAdapter;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
public class MainActivity extends Activity implements
ActionBar.OnNavigationListener {
// action bar
private ActionBar actionBar;
// Refresh menu item
private MenuItem refreshMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new SyncData.execute(); //this is example of my implementation I call my asynctask here
actionBar = getActionBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
....
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
...
case R.id.action_refresh:
// refresh
refreshMenuItem = item;
// load the data from server
new SyncData().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Async task to load the data from server
* **/
private class SyncData extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// set the progress bar view
refreshMenuItem.setActionView(R.layout.action_progressbar);
refreshMenuItem.expandActionView();
}
@Override
protected String doInBackground(String... params) {
// not making real request in this demo
// for now we use a timer to wait for sometime
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
refreshMenuItem.collapseActionView();
// remove the progress bar view
refreshMenuItem.setActionView(null);
}
};
}
因此,通過上線
refreshMenuItem.setActionView(R.layout.action_progressbar);
下面這個教程我stucked與空引用refreshMenuItem。有解決方法嗎?
是的,我給了那個asynctask一些條件,它現在的作品:) –
很高興爲你,要小心,'活動'的生命週期是一件困難的事情,有很多事情要考慮:) – Drew