本文介绍了IllegalStateException:适配器的内容,而不调用PagerAdapter#notifyDataSetChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实搜索了与此相关的所有问题,但这里找不到我的代码.

 private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
MyFragmentPagerAdapter pagerAdapter;
ArrayList<String> titles = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrollable_tabs);
    new JSONAsyncTask()
    .execute(My_Url);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.viewpager);

    viewPager.setAdapter(pagerAdapter);
    pagerAdapter.notifyDataSetChanged();
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

}

 class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONArray jsor = new JSONArray(data);

                for(int i =0; i <jsor.length();i++){

                    JSONObject obj = jsor.getJSONObject(i);


                    titles.add(obj.getString("Location"));


                /*
                 * JSONObject jsono = new JSONObject(data); JSONArray jarray
                 * = jsono.getJSONArray("Cashless");
                 *
                 * for (int i = 0; i < jarray.length(); i++) { JSONObject
                 * object = jarray.getJSONObject(i); FeedItem item = new
                 * FeedItem(); // Actors actor = new Actors();
                 * actorsList.add(object.getString("id"));
                 * item.setId(object.getInt("id")); //
                 * actor.setImage(object.getString("image")); //
                 * Log.d("image: ", object.optString("id"));
                 *
                 * // actorsList.add(actor); feedItems.add(item); }
                 */

                }
                return true;
            }

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {

        if (result == false) {

        } else {


        }
    }
}

 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{



    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public int getCount() {

        return titles.size();

    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        OneFragment myFragment = new OneFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
         notifyDataSetChanged();
        myFragment.setArguments(data);

        return myFragment;

    }

    /** Returns the number of pages */


    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position) ;

    }


    @Override
    public int getItemPosition(Object object) {
         return POSITION_NONE;
    }


}

我需要帮助来解决此问题.相同的代码适用于API 19,但最新的api 23显示错误.

这是logcat

01-20 15:19:09.621: E/AndroidRuntime(5099): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 10 Pager id: info.androidhive.materialtabs:id/viewpager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class info.androidhive.materialtabs.activity.ScrollableTabsActivity$MyFragmentPagerAdapter
解决方案

之所以发生这种情况,是因为一旦从AsyncTask获得数据,就不会刷新适配器.这是应该解决的方法.

添加pagerAdapter.notifyDataSetChanged();在onPostExecute方法中.

I did search all the question related to this but could not find any solution here is my code.

 private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
MyFragmentPagerAdapter pagerAdapter;
ArrayList<String> titles = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrollable_tabs);
    new JSONAsyncTask()
    .execute(My_Url);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.viewpager);

    viewPager.setAdapter(pagerAdapter);
    pagerAdapter.notifyDataSetChanged();
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

}

 class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONArray jsor = new JSONArray(data);

                for(int i =0; i <jsor.length();i++){

                    JSONObject obj = jsor.getJSONObject(i);


                    titles.add(obj.getString("Location"));


                /*
                 * JSONObject jsono = new JSONObject(data); JSONArray jarray
                 * = jsono.getJSONArray("Cashless");
                 *
                 * for (int i = 0; i < jarray.length(); i++) { JSONObject
                 * object = jarray.getJSONObject(i); FeedItem item = new
                 * FeedItem(); // Actors actor = new Actors();
                 * actorsList.add(object.getString("id"));
                 * item.setId(object.getInt("id")); //
                 * actor.setImage(object.getString("image")); //
                 * Log.d("image: ", object.optString("id"));
                 *
                 * // actorsList.add(actor); feedItems.add(item); }
                 */

                }
                return true;
            }

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {

        if (result == false) {

        } else {


        }
    }
}

 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{



    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public int getCount() {

        return titles.size();

    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        OneFragment myFragment = new OneFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
         notifyDataSetChanged();
        myFragment.setArguments(data);

        return myFragment;

    }

    /** Returns the number of pages */


    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position) ;

    }


    @Override
    public int getItemPosition(Object object) {
         return POSITION_NONE;
    }


}

I need help to solve this issue. Same code is working for API 19 but showing error with latest api 23. Thanks in advance

Here is the logcat

01-20 15:19:09.621: E/AndroidRuntime(5099): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 10 Pager id: info.androidhive.materialtabs:id/viewpager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class info.androidhive.materialtabs.activity.ScrollableTabsActivity$MyFragmentPagerAdapter
解决方案

This is happening because once you get the data from AsyncTask you are not refreshing your adapter. This is how you should fix it.

Add pagerAdapter.notifyDataSetChanged(); in onPostExecute method.

这篇关于IllegalStateException:适配器的内容,而不调用PagerAdapter#notifyDataSetChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 21:30