我正在尝试按顺序构建一个寻呼机来显示图像。但是我收到了这个错误。
The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
Expected adapter item count: 0, found: 1 Pager id:.
请有人指导解决。
这是我的代码:
MatChedUserProfile活动
public class MatChedUserProfile extends Activity implements OnClickListener {
private static final String TAG = "MatChedUserProfile";
private static boolean mDebugLog = true;
private static String mDebugTag = "MatChedUserProfile";
private ExtendedGallery imageExtendedGallery;
private LinearLayout image_count;
private TextView usernametextivew, ueragetextviw, distancetextview,
activetimetextview, abouttextview, abouttextviewvalues,
viewMatchedProfiletextview, statusTextView;
private RelativeLayout Aboutuseragelayout, likedislikebuttonlayout;
private Button likeButton, dislikebutton;
private ArrayList<GellaryData> imageList;
//private ImageAdapterForGellary mAdapterForGellary;
private ProgressDialog mDialog;
private int[] imageHeightandWIdth;
private ConnectionDetector cd;
private RelativeLayout.LayoutParams layoutParams;
private int count;
private TextView[] page_text;
private SharedPreferences preferences;
//new viewpager....
private ViewPager viewPager;
private MyViewPagerAdapter mAdapterForGellary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matcheduserprofile);
//Thread.setDefaultUncaughtExceptionHandler(new CrashLogger(getApplicationContext()));
cd = new ConnectionDetector(this);
if (!cd.isConnectingToInternet()) {
Toast.makeText(this, "No Internet", Toast.LENGTH_SHORT).show();
return;
}
preferences = PreferenceManager.getDefaultSharedPreferences(this);
initLayoutResource();
imageList = new ArrayList<GellaryData>();
//listOfItems=new ArrayList<GellaryData>();
mAdapterForGellary = new MyViewPagerAdapter(imageList);
//imageExtendedGallery.setAdapter(mAdapterForGellary);
viewPager.setAdapter(mAdapterForGellary);
Ultilities ultilities = new Ultilities();
imageHeightandWIdth = ultilities
.getImageHeightAndWidthForProfileGellary(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.getBoolean(Constant.isFromChatScreen)) {
likedislikebuttonlayout.setVisibility(View.GONE);
} else {
}
}
Settings.addLoggingBehavior(LoggingBehaviors.INCLUDE_ACCESS_TOKENS);
try {
likeButton.setOnClickListener(this);
dislikebutton.setOnClickListener(this);
} catch (Exception e) {
}
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
imageHeightandWIdth[1], imageHeightandWIdth[0]);
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams = ultilities.getRelativelayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.setMargins(0, 5, 0, 0);
layoutParams = ultilities.getRelativelayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.setMargins(0, 5, 0, 0);
cd = new ConnectionDetector(getApplicationContext());
if (cd.isConnectingToInternet()) {
getUserProfile();
// getUserShareeInterest();
} else {
AlertDialogManager
.internetConnetionErrorAlertDialog(MatChedUserProfile.this);
}
//myViewPagerAdapter = new MyViewPagerAdapter(imageList);
viewPager.setAdapter(mAdapterForGellary);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(viewPagerPageChangeListener);
/* imageExtendedGallery
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
for (int i = 0; i < count; i++) {
page_text[i]
.setTextColor(android.graphics.Color.GRAY);
}
page_text[pos]
.setTextColor(android.graphics.Color.LTGRAY);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});*/
}
// page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < count; i++) {
page_text[i]
.setTextColor(android.graphics.Color.GRAY);
}
page_text[position]
.setTextColor(android.graphics.Color.LTGRAY);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
private void initLayoutResource() {
viewMatchedProfiletextview = (TextView) findViewById(R.id.viewMatchedProfiletextview);
likeButton = (Button) findViewById(R.id.likeButton);
dislikebutton = (Button) findViewById(R.id.dislikebutton);
//imageExtendedGallery = (ExtendedGallery) findViewById(R.id.imageExtendedGallery);
viewPager = (ViewPager)findViewById(R.id.viewPager);
image_count = (LinearLayout) findViewById(R.id.image_count);
usernametextivew = (TextView) findViewById(R.id.usernametextivew);
ueragetextviw = (TextView) findViewById(R.id.ueragetextviw);
distancetextview = (TextView) findViewById(R.id.distancetextview);
activetimetextview = (TextView) findViewById(R.id.activetimetextview);
statusTextView = (TextView) findViewById(R.id.txtMatchedUserStatus);
// userfriendgallery = (HorizontalListView)
// findViewById(R.id.userfriendgallery);
// userIntestedgallery = (HorizontalListView)
// findViewById(R.id.userIntestedgallery);
abouttextview = (TextView) findViewById(R.id.abouttextview);
Aboutuseragelayout = (RelativeLayout) findViewById(R.id.Aboutuseragelayout);
Aboutuseragelayout.setVisibility(View.GONE);
likedislikebuttonlayout = (RelativeLayout) findViewById(R.id.likedislikebuttonlayout);
abouttextviewvalues = (TextView) findViewById(R.id.abouttextviewvalues);
Typeface HelveticaInseratLTStd_Roman = Typeface.createFromAsset(
getAssets(), "fonts/HelveticaInseratLTStd-Roman.otf");
Typeface HelveticaLTStd_Light = Typeface.createFromAsset(getAssets(),
"fonts/HelveticaLTStd-Light.otf");
viewMatchedProfiletextview.setTypeface(HelveticaLTStd_Light);
viewMatchedProfiletextview.setTextColor(Color.rgb(255, 255, 255));
viewMatchedProfiletextview.setTextSize(20);
usernametextivew.setTypeface(HelveticaInseratLTStd_Roman);
usernametextivew.setTextColor(Color.rgb(124, 124, 124));
((TextView) findViewById(R.id.txtst))
.setTypeface(HelveticaInseratLTStd_Roman);
((TextView) findViewById(R.id.txtst)).setTextColor(Color.rgb(124, 124,
124));
statusTextView.setTypeface(HelveticaInseratLTStd_Roman);
statusTextView.setTextColor(Color.rgb(124, 124, 124));
// usernametextivew.setTextSize(20);
ueragetextviw.setTypeface(HelveticaLTStd_Light);
ueragetextviw.setTextColor(Color.rgb(124, 124, 124));
abouttextview.setTypeface(HelveticaInseratLTStd_Roman);
abouttextview.setTextColor(Color.rgb(92, 92, 92));
abouttextview.setTextSize(15);
abouttextviewvalues.setTypeface(HelveticaInseratLTStd_Roman);
abouttextviewvalues.setTextColor(Color.rgb(131, 131, 131));
abouttextviewvalues.setTextSize(15);
distancetextview.setTypeface(HelveticaInseratLTStd_Roman);
distancetextview.setTextColor(Color.rgb(92, 92, 92));
distancetextview.setTextSize(15);
activetimetextview.setTypeface(HelveticaInseratLTStd_Roman);
activetimetextview.setTextColor(Color.rgb(131, 131, 131));
activetimetextview.setTextSize(15);
}
private void getUserProfile() {
SessionManager mSessionManager = new SessionManager(this);
String macheduserFacebookid = mSessionManager
.getMatchedUserFacebookId();
AppLog.Log(TAG, "Matched UserFacebook ID:" + macheduserFacebookid);
// String userSessionToken = mSessionManager.getUserToken();
String userDeviceId = Ultilities.getDeviceId(this);
if (macheduserFacebookid != null && macheduserFacebookid.length() > 0) {
// String[] params = { userSessionToken, userDeviceId,
// macheduserFacebookid };
String[] params = { macheduserFacebookid };
new BackGroundTaskForUserProfile().execute(params);
} else {
ErrorMessageMandetoryFiledMissing(
getResources().getString(R.string.alert), getResources()
.getString(R.string.retriedmessage));
}
}
private class BackGroundTaskForUserProfile extends
AsyncTask<String, Void, Void> {
Ultilities mUltilities = new Ultilities();
private String getProfileResponse;
private List<NameValuePair> userProfileNameValuePairList;
private userProFileData mUserProFileData;
private GellaryData mGellaryData;
@Override
protected Void doInBackground(String... params) {
try {
userProfileNameValuePairList = mUltilities
.getUserProfileParameter(params);
getProfileResponse = mUltilities.makeHttpRequest(
Constant.getProfile_url, Constant.methodeName,
userProfileNameValuePairList);
// logDebug("BackGroundTaskForUserProfile getProfileResponse "+getProfileResponse);
Gson gson = new Gson();
mUserProFileData = gson.fromJson(getProfileResponse,
userProFileData.class);
String[] images = mUserProFileData.getImages();
for (int i = 0; i < images.length; i++) {
mGellaryData = new GellaryData();
mGellaryData.setImageUrl(images[i]);
imageList.add(mGellaryData);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mDialog != null) {
mDialog.dismiss();
}
page_text = new TextView[imageList.size()];
count = imageList.size();
image_count.removeAllViews();
for (int i = 0; i < imageList.size(); i++) {
page_text[i] = new TextView(MatChedUserProfile.this);
page_text[i].setText(".");
page_text[i].setTextSize(45);
page_text[i].setTypeface(null, Typeface.BOLD);
page_text[i]
.setTextColor(android.graphics.Color.GRAY);
image_count.addView(page_text[i]);
}
//Log.d("shanImage", Array.toString(mUserProFileData.getImages()));
mAdapterForGellary.notifyDataSetChanged();
if (mUserProFileData.getStatus() != null
&& !mUserProFileData.getStatus().equals("")) {
statusTextView
.setText(mUserProFileData.getStatus());
} else {
statusTextView.setText("N/A");
}
ueragetextviw.setText("" + mUserProFileData.getAge());
usernametextivew.setText(""
+ mUserProFileData.getFirstName());
viewMatchedProfiletextview.setText(""
+ mUserProFileData.getFirstName());
SessionManager sessionManager = new SessionManager(
MatChedUserProfile.this);
String DistanceUinit = null;
if (sessionManager.getDistaceUnit().equals("Km")) {
DistanceUinit = "Km.";
} else {
DistanceUinit = "Mi.";
}
distancetextview.setText("Less than "
+ mUserProFileData.getDistance() + " "
+ DistanceUinit + " away");
String gmtTime = mUserProFileData.getLastActive();
// gmtTime=gmtTime.replaceAll("-", " ");
String localTime = UltilitiesDate.getLocalTime(gmtTime);
Ultilities ultilities = new Ultilities();
// String
// curentTime=ultilities.getCurrentDateYYYYMMdd();
String dataString = UltilitiesDate
.datesString(localTime);
UltilitiesDate ultilitiesDate = new UltilitiesDate();
int days = ultilitiesDate.getDays();
int hours = ultilitiesDate.getHours();
activetimetextview.setText("active " + days + " -d "
+ hours + "- Hour ago");
if (mUserProFileData.getPersDesc() != null
&& mUserProFileData.getPersDesc().length() > 0) {
Aboutuseragelayout.setVisibility(View.VISIBLE);
abouttextview.setText("About " + ""
+ mUserProFileData.getFirstName());
abouttextviewvalues.setText(""
+ mUserProFileData.getPersDesc());
} else {
Aboutuseragelayout.setVisibility(View.GONE);
}
}
});
} catch (Exception e) {
AppLog.Log(TAG,
"BackGroundTaskForUserProfile doInBackground Exception"
+ e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (mDialog != null) {
mDialog.dismiss();
}
} catch (Exception e) {
AppLog.Log(TAG,
"BackGroundTaskForUserProfile onPostExecute Exception "
+ e);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = mUltilities.GetProcessDialog(MatChedUserProfile.this);
mDialog.setMessage("Please Wait..");
mDialog.setCancelable(false);
mDialog.show();
}
}
ImageAdapterForGellary Adapter
private class ImageAdapterForGellary extends PagerAdapter {
Activity mActivity;
private LayoutInflater mInflater;
private Ultilities mUltilities = new Ultilities();
private int[] imageheightandWidth = mUltilities
.getImageHeightAndWidthForGellary(MatChedUserProfile.this);
public ImageAdapterForGellary(
List<GellaryData> objects) {
//mActivity = context;
mInflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public GellaryData getItem(int position) {
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleritem, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.thumbImage);
// holder.mProgressBar = (ProgressBar) convertView
// .findViewById(R.id.pbGalleryItemImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.mProgressBar.setId(position);
holder.imageview.setId(position);
Picasso.with(MatChedUserProfile.this) //
.load(getItem(position).getImageUrl()) //
.error(R.drawable.error) //
.resize(imageHeightandWIdth[1], imageHeightandWIdth[0]) //
.into(holder.imageview);
return convertView;
}
class ViewHolder {
ImageView imageview;
// ProgressBar mProgressBar;
}
}
MyViewPagerAdapter适配器
// adapter
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
//private ArrayList<Integer> items;
private List<GellaryData> items;
public MyViewPagerAdapter(List<GellaryData> objects) {
this.items = objects;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.galleritem, container,false);
ImageView galleimageview = (ImageView) view
.findViewById(R.id.thumbImage);
Picasso.with(MatChedUserProfile.this) //
.load(imageList.get(position).getImageUrl()) //
.error(R.drawable.error) //
.resize(imageHeightandWIdth[1], imageHeightandWIdth[0]) //
.into(galleimageview);
((ViewPager) container).addView(view);
return view;
}
@Override
public int getCount() {
return items.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View)obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View)object;
((ViewPager) container).removeView(view);
}
}
@Override
public void onStart() {
super.onStart();
// Session.getActiveSession().addCallback(statusCallback);
// FlurryAgent.onStartSession(this, Constant.flurryKey);
}
@Override
public void onStop() {
super.onStop();
// Session.getActiveSession().removeCallback(statusCallback);
// FlurryAgent.onEndSession(this);
}
}
这是我的错误:
FATAL EXCEPTION: main
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 1 Pager id: com.crushmatic.pro:id/viewPager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.fresh.slidingmenu.MatChedUserProfile$MyViewPagerAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1000)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:16677)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
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:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
at android.view.View.measure(View.java:16677)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1927)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1119)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5652)
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:5433)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
for (int i = 0; i < images.length; i++) {
mGellaryData = new GellaryData();
mGellaryData.setImageUrl(images[i]);
imageList.add(mGellaryData);
}
导致崩溃的原因是您无法将多个对象添加到连接到for循环内的适配器的数组
移动这个
mAdapterForGellary = new MyViewPagerAdapter(imageList);
//imageExtendedGallery.setAdapter(mAdapterForGellary);
viewPager.setAdapter(mAdapterForGellary);
到后执行任务
答案 1 :(得分:-1)
适配器 - &gt;
public void refreshData(ArrayList<String> path) {
mImagePath = new ArrayList<>(path);
notifyDataSetChanged();
}
活动 - &gt;
adapter.refreshData(new ArrayList<>(mImagePath));