Android 如何解决UnsupportedOperationException所需的方法destroyItem未被重写

Android 如何解决UnsupportedOperationException所需的方法destroyItem未被重写,android,android-viewpager,Android,Android Viewpager,尽管我在PagerAdapter中覆盖了destroyItem(…)在ViewPager中滑动页面时出错 未指定UnsupportedOperationException所需的方法destroyItem 凌驾 Java代码 public class PropertyPagerAdapter extends PagerAdapter { private Context _context; private int layoutId; private List<AddP

尽管我在
PagerAdapter
中覆盖了
destroyItem(…)
ViewPager中滑动页面时出错

未指定UnsupportedOperationException所需的方法destroyItem 凌驾

Java代码

public class PropertyPagerAdapter extends PagerAdapter {

    private Context _context;
    private int layoutId;
    private List<AddPropertyInfo> dataList;

    public PropertyPagerAdapter(Context context,
            int resourceId, List<AddPropertyInfo> objects) {
        // TODO Auto-generated constructor stub
        _context = context;
        layoutId = resourceId;
        dataList = objects;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return dataList.size();
    }

    @Override
    public boolean isViewFromObject(View v, Object obj) {
        // TODO Auto-generated method stub
        return v == ((View) obj);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
        ((ViewPager) container).removeView((View) object);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View pagerView = inflater.inflate(layoutId, container, false);
        TextView propertyNameTxtView = (TextView) pagerView.
                findViewById(R.id.propertyNameTxtView);
        TextView netIncomeTxtView = (TextView) pagerView.
                findViewById(R.id.netIncomeTxtView);
        TextView netIncomeValueTxtView = (TextView) pagerView.
                findViewById(R.id.netIncomeValueTxtView);
        TextView netExpTxtView = (TextView) pagerView.
                findViewById(R.id.netExpTxtView);
        TextView netExpValueTxtView = (TextView) pagerView.
                findViewById(R.id.netExpValueTxtView);
        TextView netProfitTxtView = (TextView) pagerView.
                findViewById(R.id.netProfitTxtView);
        TextView netProfitValueTxtView = (TextView) pagerView.
                findViewById(R.id.netProfitValueTxtView);

        propertyNameTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netIncomeTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netIncomeValueTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netExpTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netExpValueTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netProfitTxtView.setTypeface(Utils.getRegularTypeface(_context));
        netProfitValueTxtView.setTypeface(Utils.getRegularTypeface(_context));

        String propertyName = "";
        double netIncomeVal;
        double netExpVal;
        double netProfitVal;

        AddPropertyInfo addPropertyInfo = dataList.get(position);
        netIncomeVal = addPropertyInfo.getIncomeAmnt();
        netExpVal = addPropertyInfo.getExpAmnt();
        netProfitVal = netIncomeVal - netExpVal;
        propertyName = addPropertyInfo.getPropertyName();

        String netIncomeValStr = String.format("%.2f", netIncomeVal);
        String netExpValStr = String.format("%.2f", netExpVal);
        String netProfitValStr = String.format("%.2f", netProfitVal);

        propertyNameTxtView.setText(propertyName);
        netIncomeValueTxtView.setText(_context.getResources().
                getString(R.string.doller) + netIncomeValStr);
        netExpValueTxtView.setText(_context.getResources().
                getString(R.string.doller) + netExpValStr);
        netProfitValueTxtView.setText(_context.getResources().
                getString(R.string.doller) + netProfitValStr);

        ((ViewPager) container).addView(pagerView);

        return pagerView;
    }
}

如何解决这个问题?

不要打超级电话。除去

  super.destroyItem(container, position, object);
PagerAdapter源代码的摘录

123    public void More ...destroyItem(ViewGroup container, int position, Object object) {
124        destroyItem((View) container, position, object);
125    }

191    public void More ...destroyItem(View container, int position, Object object) {
192        throw new UnsupportedOperationException("Required method destroyItem was not overridden");
193    }
只需覆盖:

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

重写destroyItem时,应将参数中的对象从其容器(也包括参数中的对象)中移除。可以将其强制转换为相应的视图类型。 这是:

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

调用
super
时出现异常是的,不要调用super.destroyItem,我也遇到了这个问题。
super.destroyItem
只在
FragmentPagerAdapter
@Blackbelt上工作。请问您如何浏览Android源代码?安卓工作室现在似乎在这方面的设备太差了,它总是抱怨缺少一些源代码,然后当你安装它时,它崩溃了…等等
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}