IllegalStateException: 适配器的内容没有调用 PagerAdapter#notifyDataSetChanged
IllegalStateException: adapter's content without calling 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
方法中。
感谢@Rohit5k2 解决了一个愚蠢的错误。我只需要 pagerAdapter.notifyDataSetChanged.. 它可能对某些想要从服务器填充其 pagerAdapter 的人有用。
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("Your_Url");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
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"));
}
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 {
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
pagerAdapter.notifyDataSetChanged();
}
}
}
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return titles.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position) ;
}
}
}
我确实搜索了所有与此相关的问题,但找不到任何解决方案,这是我的代码。
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
方法中。
感谢@Rohit5k2 解决了一个愚蠢的错误。我只需要 pagerAdapter.notifyDataSetChanged.. 它可能对某些想要从服务器填充其 pagerAdapter 的人有用。
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("Your_Url");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
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"));
}
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 {
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
pagerAdapter.notifyDataSetChanged();
}
}
}
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return titles.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position) ;
}
}
}