gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:33:49 34 4
gpt4 key购买 nike

虽然我在 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 。删除

  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 }

关于android - 如何解决 UnsupportedOperationException 必需的方法 destroyItem 未被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26654486/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com