[英]Android how to make a thread wait until a certain event happens?
[我的状况]
现在在MainActivity中,我创建了一些片段,其中有一些TextView。 MainActivity中有一个异步线程AsyncTask,用于从Web获取信息。 AsyncTask完成获取后,它将通过回调方法更新上述TextViews中的文本(该Fragment实现一个OnInfoGotInterface并实例化接口中的onInfoGot()方法。onInfoGot()将调用Fragment中定义的setTextView()方法以更新信息。 )。
[我的问题]
执行程序时,我发现AsyncTask完成从Web获取信息的时间点比Fragment调用onCreateView()的时间点早。 换句话说,当AsyncTask调用(OnInfoGotInterface)fragment.onInfoGot()设置TextView时,尚未实例化TextView(通过rootView.findViewById()方法在CreateView()上实例化TextView)。 结果,出现了NullPointerException。
[我需要解决方案]
现在我想这样做:当AsyncTask完成从Web上获取信息并要调用onInfoGot()时,我们将其停止,并使其等待Fragment完成onCreateView()。 之后,我们唤醒AsyncTask并允许它更新片段。
PS。 有人建议我在Fragment的定义中的onCreateView()之后调用新的AsyncTask.excute()。 但是这里AsyncTask和Fragment都是在MainActivity中创建的。 它们是MainActivity中两个不同的任务线程,一个用于显示数据,另一个用于从Web获取数据。
任何人都可以提一些建议吗? 我真的很感激! 这是代码:
MainActivity.java:
public class MainActivity extends Activity{
private List<City> cityList;
private ViewPager mPager; /*ViewPager for show the page of city info*/
private ScreenSlidePagerAdapter mPagerAdapter; /* Adapter for the ViewPager,
* ScreenSlidePagerAdapter is a subclass of FragmentStatePagerAdapter*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_city);
cityList = new ArrayList<City>();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
init();
}
private void init(){
loadCities(); //Load cities from database into cityList
initFragments();
getCityInfo();
}
private void initFragments(){
mPagerAdapter.removeAllFragments();
for(City city: cityList){
PageFragment fragment = new PageFragment(city.getName());
mPagerAdapter.addFragment(fragment);
mPagerAdapter.notifyDataSetChanged();
}
}
private void getCityInfo(){
for(City city: cityList){
String cityName = city.getName();
obtainCityInfo(cityName);
}
}
private obtainCityInfo(String cityName){
String request = "http://example.abc.com/"
+ cityName + "&output=json";
new AccessWebServiceTask().execute(request, cityName);
}
private class AccessWebServiceTask extends AsyncTask<String, Void, CityInfo>{
@Override
protected CityInfo doInBackground(String... urls) {
String result = getWebContent(urls[0]); /*Access web through HTTP*/
String cityName = urls[1];
/*Transform the String result to a CityInfo object containing information of a city*/
CityInfo cityInfo = encodeJason(result, cityName);
return cityInfo;
}
protected void onPostExecute(CityInfo cityInfo){
OnCityGot(cityInfo);
}
}
public void onCityGot(CityInfo cityInfo){
if(cityInfo != null){
String cityName = cityInfo.getCityName();
/*Set the info field of a city object*/
cityList.getByName(cityName).setCityInfo(cityInfo);
mPagerAdapter.updateFragment(cityInfo);
}
}
}
ScreenSlidePagerAdapter.java
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
private List<PageFragment> fragmentsList;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
fragmentsList = new ArrayList<PageFragment>();
}
public ScreenSlidePagerAdapter(FragmentManager fm, List<PageFragment> list){
super(fm);
fragmentsList = list;
}
@Override
public PageFragment getItem(int position) {
return fragmentsList.get(position);
}
@Override
public int getCount() {
return fragmentsList.size();
}
public void setFragmentsList(List<PageFragment> fragmentsList){
this.fragmentsList = fragmentsList;
notifyDataSetChanged();
}
public void addFragment(PageFragment f){
fragmentsList.add(f);
notifyDataSetChanged();
}
public void removeFragment(int position){
fragmentsList.remove(position);
notifyDataSetChanged();
}
public void removeAllFragments(){
fragmentsList.clear();
notifyDataSetChanged();
}
private PageFragment findFragmentByName(String cityName){
for(PageFragment fragment: fragmentsList){
if(fragment.getCityName().equals(cityName))
return fragment;
}
return null;
}
public void updateFragment(CityInfo cityInfo){
String cityName = cityInfo.getCityName();
OnCityInfoChanged fragment = (OnCityInfoChanged)findFragmentByName(cityName);
String population = cityInfo.getPopulation();
fragment.onCityInfoChanged(population);
notifyDataSetChanged();
}
}
PageFragment.java:
public class PageFragment extends Fragment implements OnCityInfoChanged{
private TextView cityNameText;
private TextView populationText;
String cityName;
String population;
public PageFragment(){}
public PageFragment(String cityName, String population){
this.cityName = cityName;
this.population = population
}
public PageFragment(CityInfo cityInfo){
this.cityName = cityInfo.getCityName();
this.population = cityInfo.getPopulation();
}
public PageFragment(String cityName){
this.cityName = cityName;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
setCityName(cityName);
setPopulation(population)
return rootView;
}
public void setCityName(String name){
cityNameText.setText(name);
}
public void setPopulation(String population){
populationText.setText(population);
}
public String getCityName(){
return cityName;
}
@Override
public void onCityInfoChanged(String population) {
//setCityName(cityName);
setPopulation();
}
}
您可以尝试ConditionVariable ,它可以用于保存AsyncTask
private ConditionVariable mCondition = new ConditionVariable(false);
mCondition.block(); //used to block
// your condition
mCondition.open(); // used to open
首先,您不应该将Fragment构造函数与参数一起使用。 您应该将捆绑包中的片段的参数发送给片段参数集。 参见: https : //stackoverflow.com/a/15392591/360211
我将在其onCreate
obtainCityInfo
其片段称为obtainCityInfo
方法。 然后,不再进行查找,我可以将OnCityInfoChanged
传递给异步任务,以使其调用onPostExecute
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
setCityName(cityName);
obtainCityInfo(cityName, this);
return rootView;
}
private obtainCityInfo(String cityName, OnCityInfoChanged callback){
String request = "http://example.abc.com/"
+ cityName + "&output=json";
new AccessWebServiceTask(callback).execute(request, cityName);
}
我会建议删除的AsyncTask AccessWebServiceTask
从在MainActivity并把它放在片段PageFragment
。 然后在此片段中,重写onActivityCreated
,启动AsyncTask。
[编辑]
这是您代码的更新版本。 测试它是否有效:
主要活动
public class MainActivity extends Activity{
private List<City> cityList;
private ViewPager mPager; /*ViewPager for show the page of city info*/
private ScreenSlidePagerAdapter mPagerAdapter; /* Adapter for the ViewPager,
* ScreenSlidePagerAdapter is a subclass of FragmentStatePagerAdapter*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_city);
cityList = new ArrayList<City>();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
init();
}
private void init(){
loadCities(); //Load cities from database into cityList
initFragments();
}
private void initFragments(){
mPagerAdapter.removeAllFragments();
for(City city: cityList){
PageFragment fragment = PageFragment.newFragment(city.getName());
mPagerAdapter.addFragment(fragment);
}
}
}
ScreenSlidePagerAdapter:
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
private List<PageFragment> fragmentsList;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
fragmentsList = new ArrayList<PageFragment>();
}
public ScreenSlidePagerAdapter(FragmentManager fm, List<PageFragment> list){
super(fm);
fragmentsList = list;
}
@Override
public PageFragment getItem(int position) {
return fragmentsList.get(position);
}
@Override
public int getCount() {
return fragmentsList.size();
}
public void setFragmentsList(List<PageFragment> fragmentsList){
this.fragmentsList = fragmentsList;
notifyDataSetChanged();
}
public void addFragment(PageFragment f){
fragmentsList.add(f);
notifyDataSetChanged();
}
public void removeFragment(int position){
fragmentsList.remove(position);
notifyDataSetChanged();
}
public void removeAllFragments(){
fragmentsList.clear();
notifyDataSetChanged();
}
}
PageFragment:
public class PageFragment extends Fragment {
private TextView cityNameText;
private TextView populationText;
private String cityName;
private String population;
public static final String CITY_NAME_KEY = "cityname";
public PageFragment(){}
public static PageFragment newFragment(String cityName){
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(CITY_NAME_KEY, cityName);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate();
if(savedInstanceState != null){
this.cityName = savedInstanceState.getString(CITY_NAME_KEY);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
if(this.cityName != null){
String request = "http://example.abc.com/" + cityName + "&output=json";
new AccessWebServiceTask().execute(request, cityName);
}
}
public void setCityName(String name){
cityNameText.setText(name);
}
public void setPopulation(String population){
populationText.setText(population);
}
public String getCityName(){
return cityName;
}
private class AccessWebServiceTask extends AsyncTask<String, Void, CityInfo>{
@Override
protected CityInfo doInBackground(String... urls) {
String result = getWebContent(urls[0]); /*Access web through HTTP*/
String cityName = urls[1];
/*Transform the String result to a CityInfo object containing information of a city*/
CityInfo cityInfo = encodeJason(result, cityName);
return cityInfo;
}
protected void onPostExecute(CityInfo cityInfo){
OnCityGot(cityInfo);
}
}
public void onCityGot(CityInfo cityInfo){
if(cityInfo != null){
String population = cityInfo.getPopulation();
setPopulation(population);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.