how to resolve UnsupportedOperationException Required method destroyItem was not overridden

Though I have override destroyItem(...) in PagerAdapter getting an error while sliding pages in ViewPager

UnsupportedOperationException Required method destroyItem was not
overridden

  • Can I have an android activity run only on the first time an application is opened?
  • Indic Language Support for SDK but not PhoneGap
  • How to run my node.js project on android?
  • Android Mediaplayer: setDataSource issue for downloaded media file
  • How to parse JSON in Android
  • DrawerLayout ListView not drawn with GLSurfaceView as content
  • Java code

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

    Error log

    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)
    

    How to resolve it?

    Related posts:

    Getting statistics from Google Play Developers with an API
    How to specify the JDK version in android studio?
    create json in android
    Notification background in Android Lollipop is white. How can I change it?
    Some 3rd party widgets stop updating after application upgrade
    How to Access Camera in Android 6.0 Marshmallow?
  • Automatically prevent apps starting from the launcher
  • JQuery doesn't work on WebView
  • OnItemClickListener Not Triggered on Android GridView
  • EditText request focus not working
  • Items in ActionBarCompat are showed always in Overflow
  • Unable to debug cordova app due to adb.exe
  • 3 Solutions collect form web for “how to resolve UnsupportedOperationException Required method destroyItem was not overridden”

    Don’t call the super. Remove

      super.destroyItem(container, position, object);
    

    An extract of the PagerAdapter source code

    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    }
    

    Just override:

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

    When you override destroyItem, you should remove the object getted in the parameter from its container(also getted in parameters). You can cast it to the corresponding View type.
    Here it is:

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.