java.lang.IllegalStateException:应用程序';s PagerAdapter已更改适配器';s内容而不调用PagerAdapter#notifyDataSetChanged
我有一个扩展pageradapter的滑动图像。起初它是有效的。但当我打开“新活动”,然后按“上一步”按钮时,应用程序正在崩溃,我出现以下错误:java.lang.IllegalStateException:应用程序';s PagerAdapter已更改适配器';s内容而不调用PagerAdapter#notifyDataSetChanged,java,android,Java,Android,我有一个扩展pageradapter的滑动图像。起初它是有效的。但当我打开“新活动”,然后按“上一步”按钮时,应用程序正在崩溃,我出现以下错误: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 10, found: 0 Pager id: malate.denise.chelsie.igphthesisbfinal:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class malate.denise.chelsie.igphthesisbfinal.SlidingImage_Adapter
这是我的SlidingImage_Adapter.class
public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<Pictures> IMAGES;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context,ArrayList<Pictures> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.slidingimage_layout, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
new DownloadImageTask(imageView).execute(IMAGES.get(position).getPhotopath());
// imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
notifyDataSetChanged();
}
@Override
public Parcelable saveState() {
return null;
}
公共类SlidingImage_适配器扩展了PagerAdapter{
私有数组列表图像;
私人充气机;
私人语境;
公共幻灯片图像适配器(上下文、ArrayList图像){
this.context=上下文;
这个。图像=图像;
充气器=充气器。从(上下文);
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((视图)对象);
}
@凌驾
public int getCount(){
返回图像。size();
}
@凌驾
公共对象实例化项(视图组视图,int位置){
查看图像布局=充气机。充气(R.layout.slidingimage\u布局,视图,false);
断言imageLayout!=null;
最终图像视图图像视图=(图像视图)图像布局
.findviewbyd(R.id.image);
新建下载的ImageTask(imageView).execute(IMAGES.get(position.getPhotopath());
//setImageResource(IMAGES.get(position));
view.addView(imageLayout,0);
返回图像布局;
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回视图.equals(对象);
}
@凌驾
公共无效恢复状态(可包裹状态,类装入器){
notifyDataSetChanged();
}
@凌驾
公共包裹存储状态(){
返回null;
}
还有我的AttractionInfo.class
public class AttractionInfo extends ActionBarActivity implements View.OnClickListener {
TextToSpeech tts;
TextView nameofplace,location,exact_address,operatinghours,info;
String nameofplace_s,location_s,exact_address_s,operatinghours_s,info_s,latlang_s,path_s,category_s;
ImageView play,rate,addtoplan,map;
ArrayList<Pictures> records;
private static ViewPager mPager;
private static int currentPage = 0;
String line=null;
private static int NUM_PAGES = 0;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attraction_info);
map = (ImageView)findViewById(R.id.map);
map.setOnClickListener(this);
records=new ArrayList<Pictures>();
nameofplace = (TextView) findViewById(R.id.nameofplace);
info = (TextView) findViewById(R.id.info);
exact_address = (TextView) findViewById(R.id.exact_address);
location = (TextView)findViewById(R.id.location);
operatinghours=(TextView)findViewById(R.id.operatinghours);
play = (ImageView) findViewById(R.id.play);
rate = (ImageView) findViewById(R.id.rate);
addtoplan = (ImageView)findViewById(R.id.addtoplan);
addtoplan.setOnClickListener(this);
Bundle extras = getIntent().getExtras();
play.setOnClickListener(this);
rate.setOnClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
getSupportActionBar().setDisplayShowTitleEnabled(false);
tts = new TextToSpeech(AttractionInfo.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR){
tts.setLanguage(Locale.ENGLISH);
}
}
});
if (extras != null) {
nameofplace_s = extras.getString("name");
nameofplace.setText(nameofplace_s);
info_s = extras.getString("description");
info.setText(info_s);
exact_address_s = extras.getString("exactaddress");
exact_address.setText(exact_address_s);
location_s = extras.getString("location");
location.setText(location_s);
operatinghours_s=extras.getString("operatinghours");
operatinghours.setText(operatinghours_s);
latlang_s = extras.getString("latlang");
path_s=extras.getString("path");
category_s = extras.getString("category");
}
}
public void onStart() {
super.onStart();
fetchphoto bt = new fetchphoto();
bt.execute();
}
@Override
protected void onPause() {
if (tts != null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
@Override
public void onClick(View v) {
if (v == play) {
String speech1 = nameofplace.getText().toString();
String speech2 = info.getText().toString();
String speech = speech1 + "." + speech2;
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
} else if (v==rate){
Intent myIntent = new Intent(this, ListOfReviews.class);
myIntent.putExtra("placename", nameofplace_s);
myIntent.putExtra("category",category_s);
startActivity(myIntent);
}else if(v==addtoplan){
Intent myIntent = new Intent(this, AddPlan.class);
myIntent.putExtra("placename", nameofplace_s);
myIntent.putExtra("category",category_s);
startActivity(myIntent);
}else if (v == map){
Intent myIntent = new Intent(this, Map.class);
myIntent.putExtra("placename", nameofplace_s);
myIntent.putExtra("category",category_s);
myIntent.putExtra("latlang",latlang_s);
myIntent.putExtra("address",exact_address_s);
myIntent.putExtra("path",path_s);
startActivity(myIntent);
}
}
private void init() {
SlidingImage_Adapter adapter = new SlidingImage_Adapter(this,records);
for(int i=0;i<records.size();i++){
Pictures pics = records.get(i);
String pic = pics.getPhotopath();
adapter.notifyDataSetChanged();
}
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(adapter);
CirclePageIndicator indicator = (CirclePageIndicator)
findViewById(R.id.indicator);
indicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
indicator.setRadius(5 * density);
NUM_PAGES =records.size();
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
},5000, 5000);
// Pager listener over indicator
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
private class fetchphoto extends AsyncTask<Void, Void, Void> {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(Void... params) {
InputStream is = null;
String result = "";
nameValuePairs.add(new BasicNameValuePair("cityname", nameofplace_s));
try
{
records.clear();
HttpParams httpParams = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://igph.esy.es/getphoto.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
//parse json data
try {
Log.i("tagconvertstr", "["+result+"]");
// Remove unexpected characters that might be added to beginning of the string
result = result.substring(result.indexOf(""));
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Pictures p = new Pictures();
p.setPhotoname(json_data.getString("photo_name"));
p.setPlacename(json_data.getString("place_name"));
p.setCategory(json_data.getString("category"));
p.setPhotopath(json_data.getString("path"));
records.add(p);
}
} catch (Exception e) {
Log.e("ERROR", "Error pasting data " + e.toString());
}
return null;
}
protected void onPostExecute(Void result) {
// if (pd != null) pd.dismiss(); //close dialog
Log.e("size", records.size() + "");
init();
}
}
}
公共类AttractionInfo扩展ActionBarActivity实现View.OnClickListener{
texttospeechtts;
text查看地点名称、位置、确切地址、操作时间、信息;
位置、位置、确切地址、操作小时、信息、latlang、路径、类别的字符串名称;
图像视图播放、速率、添加计划、地图;
ArrayList记录;
专用静态寻呼机;
私有静态int currentPage=0;
字符串行=null;
私有静态int NUM_PAGES=0;
工具栏;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u attraction\u info);
map=(ImageView)findviewbyd(R.id.map);
map.setOnClickListener(this);
记录=新的ArrayList();
nameofplace=(TextView)findViewById(R.id.nameofplace);
info=(TextView)findViewById(R.id.info);
精确地址=(TextView)findViewById(R.id.exact\u地址);
位置=(TextView)findViewById(R.id.location);
operatinghours=(TextView)findViewById(R.id.operatinghours);
play=(ImageView)findViewById(R.id.play);
速率=(ImageView)findViewById(R.id.rate);
addtoplan=(ImageView)findViewById(R.id.addtoplan);
addtoplan.setOnClickListener(此);
Bundle extras=getIntent().getExtras();
play.setOnClickListener(这个);
rate.setOnClickListener(此);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
工具栏.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
onBackPressed();
}
});
getSupportActionBar().setDisplayShowTitleEnabled(false);
tts=new TextToSpeech(AttractionInfo.this,new TextToSpeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
if(状态!=TextToSpeech.ERROR){
tts.setLanguage(Locale.ENGLISH);
}
}
});
如果(附加值!=null){
name of place_s=extras.getString(“名称”);
地点名称。setText(地点名称);
info_s=extras.getString(“说明”);
info.setText(信息);
精确地址=extras.getString(“精确地址”);
确切地址.setText(确切地址);
location_s=extras.getString(“location”);
location.setText(位置);
operatinghours_s=extras.getString(“operatinghours”);
setText(操作小时数);
latlang_s=extras.getString(“latlang”);
path_s=extras.getString(“path”);
category_s=extras.getString(“category”);
}
}
public void onStart(){
super.onStart();
fetchphoto bt=新的fetchphoto();
bt.execute();
}
@凌驾
受保护的void onPause(){
如果(tts!=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
@凌驾
公共void onClick(视图v){
如果(v==播放){
String speech1=nameofplace.getText().toString();
String speech2=info.getText().toString();
字符串语音=speech1+“+”speech2;
tts.speak(speech,TextToSpeech.QUEUE\u FLUSH,null);
}否则如果(v==费率){
Intent myIntent=newintent(这是ListOfReviews.class);
myIntent.putExtra(“地名”,地名);
myIntent.putExtra(“类别”,类别);
星触觉(myIntent);
}else if(v==addtoplan){
Intent myIntent=新的Intent(this,AddPlan.class);
myIntent.putExtra(“地名”,地名);
myIntent.putExtra(“类别”,类别);
星触觉(myIntent);
}else if(v==map){
Intent myIntent=新Intent(这个,Map.class);
myIntent.putExtra(“地名”,地名);
myIntent.putExtra(“类别”,类别);
myIntent.putExtra(“latlang”,latlang_);
myIntent.putExtra(“地址”,确切地址);
myIntent.putExtra(“路径”,路径);
星触觉(myIntent);
}
}
私有void init(){
滑动图像_适配器=n