我确实搜索了与此相关的所有问题,但在这里找不到任何解决方案是我的代码。
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
答案 0 :(得分:2)
这种情况正在发生,因为一旦从AsyncTask
获取数据,您就无法刷新适配器。这是你应该如何修复它。
添加pagerAdapter.notifyDataSetChanged()
;在onPostExecute
方法中。
答案 1 :(得分:0)
由于@ 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) ;
}
}
}