0

我有以下问题。我有一个PagerAdapter应该在后台动态加载页面(图片)的内容......首先 viewpager 加载了一个页面。然后异步任务从用户拥有的图片中获取特定用户 ID 的所有 URL...然后 PagerAdapter 应该创建与图片将要加载一样多的默认页面..最后图片应该通过异步任务加载..

主要活动:

public class MainActivity extends Activity {
String[] urls;
ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
GalleryPagerAdapter mAdapter = new GalleryPagerAdapter(this, urls, pics);

public void savePicUrls(String result){
    String[] urls = result.toString().split(",");

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

// This asyncTask starts in his onPostExecute method, the method "savePic" in the MainActivity with a single Bitmap
        List_getImage test = new List_getImage(urls[i], this);
        test.execute();
    }
}

public void savePic(Bitmap result){
    pics.add(result);
    //mAdapter = new GalleryPagerAdapter(this, urls, pics);
    mAdapter.notifyDataSetChanged();    
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout_activity);
    GalleryPagerAdapter adapter = new GalleryPagerAdapter(this, urls, pics);
    ViewPager myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

寻呼机适配器:

public class GalleryPagerAdapter extends PagerAdapter {
String USID = "2";
int count=1;
String url="http://193.171.131.105/prein/bluepeople/SucheFotos_PREIN.php";
MainActivity actRef;
String[] urls;
ArrayList<Bitmap> pics;

public GalleryPagerAdapter(MainActivity actRef, String[] urls, ArrayList<Bitmap> pics) {
    this.actRef = actRef;
    this.urls = urls;
    this.pics = pics;
}

public void setCount(int PageCount){
    count = count + PageCount;
    System.out.println(count);
}

public int getCount() {
    return pics.size() + 1;
}

public Object instantiateItem(View collection, int pos) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View page = inflater.inflate(R.layout.fragment_screen_slide_page, null);
    ImageView iv = (ImageView) page.findViewById(R.id.up_pub_iv_viewpager);
    if(pos == 0){
        iv.setImageResource(R.drawable.ic_launcher);
    }
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("USID", USID));

// This asyncTask starts in his onPostExecute method, the method "savePicUrls" in the     MainActivity with a String with all the URLs of the pictures
        UserProfilePub_getImageUrl upp_getImageUrls = new UserProfilePub_getImageUrl(nameValuePair, url, actRef, iv);
        upp_getImageUrls.execute();

    if(pics.size() != 0){
        iv.setImageBitmap(pics.get(pos));
    }

    ((ViewPager) collection).addView(page, 0);
    return page;


}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

我认为一切都应该正常工作......但它不会......应用程序崩溃并出现“IllegalStateException...... Pagerscontent 在没有调用.notifyDataSetChanged”的情况下被更改,但我完全在“savePic”方法中这样做......我不知道我的错误在哪里...至少还有其他方法可以获取在后台动态加载图片并添加默认页面的viewpager吗?提前致谢!!

4

1 回答 1

0

你在哪里调用 savePic()?

如果您从 ASyncTask 的 doInBackground() 调用它,这可能是问题所在。错误消息有点奇怪,但我想当适配器从非 UI 线程更改时我看到了此消息。

如果是这种情况,请在 doInBackground() 中使用 publishProgress() 并在 AsyncTask 的 onProgressUpdate() 中调用 savePic。(或收集doInbackground中的所有图片并处理onPostExecute的添加)

如果不是这种情况,请告诉我您在哪里调用 savePic

于 2013-11-13T13:33:46.437 回答