PagerAdapter

时间:2015-05-12 03:11:21

标签: java android fragmentpageradapter notifydatasetchanged

这种例外有时会发生,而且会有一些电话。

例外:

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 5 Pager id: com.cutv.mobile.zshdclient:id/vp_content_newslist Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.cutv.mobile.zs.hdclient.activity.NewsListActivity$CategoryAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:967)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16508)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16508)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16508)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16508)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16508)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1994)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1373)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5687)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5014)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
at dalvik.system.NativeStart.main(Native Method)

守则:

public class NewsListActivity extends BaseActivity implements
    View.OnClickListener {

private Intent mSuperIntent;
private PageIndicator mIndicator;
private ViewPager mViewPager;
private List<NewsFragment> mList;
private CategoryAdapter mAdapter;
private int mTitle;
private ProgressBar mWait_pb;
private CategoryLoadTask cdt;
private String tag = "News";

@Override
protected int setLayout() {
    return R.layout.activity_newslist;
}
@Override
protected void initBase() {
    mWait_pb = (ProgressBar) findViewById(R.id.pb_wait_newslist);
    mIndicator = (PageIndicator) findViewById(R.id.indicator_newslist);
    mViewPager = (ViewPager) findViewById(R.id.vp_content_newslist);
    mList = new ArrayList<NewsFragment>();
    LogUtils.printDebug(tag, mList.size() + "first");
    cdt = new CategoryLoadTask();
    cdt.execute();
    LogUtils.printDebug(tag, mList.size() + "four");
}
@Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.btn_back_title) {
        finishCurrentActivity();
        cdt.cancel(true);
        mList.clear();
        mList=null;
        mViewPager.clearFocus();
        mViewPager=null;
        mIndicator =null;
    }
}

private class CategoryAdapter extends FragmentPagerAdapter {

    public CategoryAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        LogUtils.printDebug(tag, mList.size() + "five");
        return mList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mList.get(position).getInfo().title;
    }

    @Override
    public Fragment getItem(int arg0) {
        return mList.get(arg0);
    }
}
private class CategoryLoadTask extends AsyncTask<Object, Void, Void> {

    private String scontent;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mWait_pb.setVisibility(View.VISIBLE);
        mAdapter = new CategoryAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mIndicator.setViewPager(mViewPager);
    }

    @Override
    protected Void doInBackground(Object... params) {
        String categoryType = "news";
        if (mTitle == R.string.life) {
            categoryType = "life";
        } else if (mTitle == R.string.goverment_affairs) {
            categoryType = "gov";
        }
        scontent = WAPI.get_content_from_remote_url(
                WAPI.WAPI_GET_CATEGORY_URL + categoryType, 10000);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        WAPI.parse_category_content(scontent, mList);
        LogUtils.printDebug(tag, mList.size() + "Third");
        mAdapter.notifyDataSetChanged();
        mIndicator.notifyDataSetChanged();
        mWait_pb.setVisibility(View.GONE);
    }
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    cdt.cancel(true);
    mList.clear();
    mList=null;
    mViewPager.clearFocus();
    mViewPager=null;
    mIndicator =null;

}

}

0 个答案:

没有答案