Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 721
Kaushik Avatar asked Oct 30 '14 13:10

Kaushik


3 Answers

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    }
like image 97
Blackbelt Avatar answered Oct 26 '22 11:10

Blackbelt


Just override:

@Override
public void destroyItem(View container, int position, Object object) {
     ((ViewPager) container).removeView((View) object);
}
like image 35
Dmitry Dudá Avatar answered Oct 26 '22 10:10

Dmitry Dudá


When you override destroyItem, you should remove the object got in the parameter from its container(also got 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);
}
like image 6
Ernesto Vega Avatar answered Oct 26 '22 10:10

Ernesto Vega