[我的情况]
现在在MainActivity中,我创建了一些片段,其中有一些TextView。并且MainActivity中有一个异步线程AsyncTask来从Web获取信息。在AsyncTask完成获取之后,它将通过回调方法更新上面提到的TextViews中的文本(Fragment实现OnInfoGotInterface并在接口中实现onInfoGot()方法.onInfoGot()将调用Fragment中定义的方法setTextView()来更新信息)。
[我的问题]
执行程序时,我发现AsyncTask完成从web获取信息的时间点早于Fragment调用onCreateView()的时间点。换句话说,当AsyncTask调用(OnInfoGotInterface)fragment.onInfoGot()来设置TextViews时,TextViews还没有实现(TextViews是通过rootView.findViewById()方法在CreateView()上实现的)。结果,出现了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();
}
}
答案 0 :(得分:0)
您可以尝试ConditionVariable,它可用于保存AsyncTask
private ConditionVariable mCondition = new ConditionVariable(false);
mCondition.block(); //used to block
// your condition
mCondition.open(); // used to open
答案 1 :(得分:0)
首先,您不应该使用带参数的Fragment构造函数。您应该将参数发送到bundle set中的片段到fragment参数。请参阅:https://stackoverflow.com/a/15392591/360211
我会让片段调用obtainCityInfo
中的onCreate
方法。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);
}
上调用。
{{1}}
答案 2 :(得分:0)
我建议从MainActivity中删除AsyncTask AccessWebServiceTask
并将其放入片段PageFragment
中。然后在这个片段中,覆盖onActivityCreated
,启动AsyncTask。
[编辑]
以下是您的代码的更新版本。测试它是否有效:
MainActivity
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);
}
}
}