我正在尝试在ASyncTask中获取一些数据,一旦完成将数据传递给viewpager中的任何片段。但我得到以下例外:
java.lang.IllegalStateException:尚未绑定ViewPager。
我必须遵循以下代码:
OnCreateView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.simple_tabs);
team_id = this.getIntent().getIntExtra("index", 0) + 1;
new getTeam().execute();
}
的AsyncTask:
@Override
protected Void doInBackground(Void... arg0) {
team = api.getTeamByID(team_id);
return null;
}
@Override
protected void onPostExecute(Void result) {
mAdapter = new TeamFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
progressDialog.dismiss();
}
TeamFragmentAdapter:
class TeamFragmentAdapter extends FragmentPagerAdapter implements TitleProvider {
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
ArrayList<String> titels = new ArrayList<String>();
public TeamFragmentAdapter(FragmentManager fm) {
super(fm);
fragments.add(new TeamInformatieFragment(team));
titels.add("Informatie");
fragments.add(new ContactFragment());
titels.add("Informatie");
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public String getTitle(int position) {
return titels.get(position);
}
}
当我将“OnPostExecute(Void result)”的内容移动到OnCreateView时,它可以工作......但是我必须在创建片段之前获取所有数据。
你能告诉我出了什么问题吗? :(
答案 0 :(得分:8)
修复!我不得不将所有布局内容从ASyncTask移到OnPostExecute。 它看起来像这样:
@Override
protected void onPostExecute(Void result) {
setContentView(R.layout.simple_tabs);
mAdapter = new TeamFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
invalidateOptionsMenu();
progressDialog.dismiss();
}