如何使用glidev4在Viewpager中使用PhotoView处理StackOveflowError:堆栈大小8MB?

时间:2018-06-27 19:00:04

标签: java android-viewpager android-glide android-photoview

当尝试使用photoView库在ViewPager活动中滑动时出现此错误,我对图像使用Glide,并且url来自Intent(url为数组)。我还使用模型类从firebase数据库下载数据。

enter image description here

这是日志:

06-27 14:36:33.291 6520-6520/com.realty.drake.kunuk E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.realty.drake.kunuk, PID: 6520
java.lang.StackOverflowError: stack size 8MB
    at android.support.v4.view.PagerAdapter.startUpdate(PagerAdapter.java:96)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1078)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:3089)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:284)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:45)
    at com.realty.drake.kunuk.ViewPagerActivity$SamplePagerAdapter.instantiateItem(ViewPagerActivity.java:34)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.vie
  06-27 14:36:33.411 6520-6520/com.realty.drake.kunuk E/JavaBinder: !!!     FAILED BINDER TRANSACTION !!!  (parcel size = 1275500)
  06-27 14:36:33.412 6520-6520/com.realty.drake.kunuk E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException: data parcel size 1275500 bytes
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(Binder.java:503)
    at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4447)
    at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
    at  com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.uncaughtExcep tion(CrashlyticsUncaughtExceptionHandler.java:51)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

ViewPager活动,在那里我从意图中获取url,将其解析为一个数组,因为它是多个字符串连接在一起的。滑行使用它来显示图像。第一个图像显示并没有崩溃,但是当我滑动到第二个图像时,花了几秒钟然后崩溃(仅活动崩溃而不是应用程序崩溃),但是如果我从第二个图像滑动到第三个图像,它会突然崩溃甚至显示它。

public class ViewPagerActivity extends AppCompatActivity {

String imageList = null;
String[] propertyImageArray = {};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_pager);
    //get imageList url from Intent
    imageList = getIntent().getStringExtra("imageList");
    propertyImageArray = imageList.split(",");
    ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(new SamplePagerAdapter());
}

class SamplePagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return propertyImageArray.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        //Must call notifyDataSetChanged before adding imageList to View
        //otherwise it'll crash
        notifyDataSetChanged();
        PhotoView photoView = new PhotoView(ViewPagerActivity.this);
        GlideApp.with(ViewPagerActivity.this)
                .load(propertyImageArray[position])
                .fitCenter()
                .into(photoView);

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;


    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

这是将意图发送到ViewPagerActivity的类,

public class PropertyLotDetail extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {


@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.property_lot_detail);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.kunuk_toolbar);
    myToolbar.inflateMenu(R.menu.menu_detail);
    setSupportActionBar(myToolbar);

    myToolbar.setOnMenuItemClickListener(this);

    //collect our intent
    Intent intent = getIntent();
    Property property = intent.getParcelableExtra("Property");

    //collect all property values from Parcelable
    int price = property.getPrice();
    String address = property.getAddress();
    final  String propertyImage = property.getPropertyImage();
    float lotDim = property.getLotDim();
    String propertyDesc = property.getPropertyDesc();
    String lotTerrainType = property.getLotTerrainType();
    String lotTerrainDimExt = property.getLotTerrainDimExt();
    String lotTerrainSteepness = property.getLotTerrainSteepness();
[...]
 //Bind the data from the Parcelable to the Views
    TextView addressDetail = findViewById(R.id.post_address);
    addressDetail.setText(String.valueOf(address));

    String currencyPrice = NumberFormat //Format the price variable in currency form
            .getCurrencyInstance(Locale.US)
            .format(price);
    TextView priceDetail = findViewById(R.id.post_price);
    priceDetail.setText(currencyPrice);

    ImageView imageView = findViewById(R.id.post_propertyImage);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(),
                    ViewPagerActivity.class)
                    .putExtra("imageList", propertyImage)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

        }
    });

    //take one long string containing multiple url and parse it
    String propertyImageArray[] = propertyImage.split(",");

    //TODO add loading icon for placeholder

    // Download directly from StorageReference using Glide
    // (See MyAppGlideModule for Loader registration)
    GlideApp.with(getApplication())
            .load(propertyImageArray[0])
            .fitCenter()
            .into(imageView);

这是最相关的信息。请帮助

1 个答案:

答案 0 :(得分:0)

我刚刚摆脱了notifyDataChanged()方法调用。我想它已经被自动调用了,我不需要这样做,这就是为什么堆栈跟踪会导致无限递归的原因。这就是我的想法。

public class ViewPagerActivity extends AppCompatActivity {

String imageList = null;
String[] propertyImageArray = {};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
//get imageList url from Intent
imageList = getIntent().getStringExtra("imageList");
propertyImageArray = imageList.split(",");
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());
}

class SamplePagerAdapter extends PagerAdapter {

@Override
public int getCount() {
    return propertyImageArray.length;
}

@Override
public View instantiateItem(ViewGroup container, int position) {
    //Must call notifyDataSetChanged before adding imageList to View
    //otherwise it'll crash

notifyDataSetChanged();

    PhotoView photoView = new PhotoView(ViewPagerActivity.this);
    GlideApp.with(ViewPagerActivity.this)
            .load(propertyImageArray[position])
            .fitCenter()
            .into(photoView);

    // Now just add PhotoView to ViewPager and return it
    container.addView(photoView,
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    return photoView;


}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

对不起,格式不好,不知道如何在代码中制作删除线