虽然我在PagerAdapter中覆盖了destroyItem(…),在ViewPager中滑动页面时收到错误
UnsupportedOperationException required method destroyItem was not
overridden
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 }