android – 如何解决UnsupportedOperationException必需的方法destroyItem未被覆盖

虽然我在PagerAdapter中覆盖了destroyItem(…),在ViewPager中滑动页面时收到错误

UnsupportedOperationException required method destroyItem was not
overridden

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;
    }
}

错误日志

10-30 18:59:11.711: E/AndroidRuntime(27093): FATAL EXCEPTION: main
10-30 18:59:11.711: E/AndroidRuntime(27093): Process: com.teklabs.portfoliomanagenent,PID: 27093
10-30 18:59:11.711: E/AndroidRuntime(27093): java.lang.UnsupportedOperationException: required method destroyItem was not overridden
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:192)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:124)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at com.teklabs.portfoliomanagenent.adapter.PropertyPagerAdapter.destroyItem(PropertyPagerAdapter.java:47)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1002)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.view.Choreographer.doFrame(Choreographer.java:572)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.os.Handler.handleCallback(Handler.java:733)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.os.Looper.loop(Looper.java:157)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at android.app.ActivityThread.main(ActivityThread.java:5356)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at java.lang.reflect.Method.invoke(Method.java:515)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-30 18:59:11.711: E/AndroidRuntime(27093):    at dalvik.system.NativeStart.main(Native Method)

如何解决

解决方法

不要叫超级去掉
super.destroyItem(container,object);

PagerAdapter源代码的摘录

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

191    public void More ...destroyItem(View container,Object object) {
192        throw new UnsupportedOperationException("required method destroyItem was not overridden");
193    }

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...