Android SwipeLayout会导致应用在滚动时崩溃

时间:2017-11-22 16:07:11

标签: android android-layout

我通过滑动显示列表中的所有项目。但是当我滚动它会崩溃我的应用程序。我打电话给 closeAllItems(),但它没有帮助。

这是一个简单的 ListView ListView 位于片段

这是我打电话给 SwipeLayout

的地方
listView = (ListView) rootview.findViewById(R.id.con_list);
        StoreContacts = new ArrayList<String>();
        contactid=new ArrayList<String>();
        adapter=getAdapter(rootview);
        final ViewContactAdapter viewContactAdapter=new ViewContactAdapter(rootview.getContext(),contactid.size());
        listView.setAdapter(viewContactAdapter);
        viewContactAdapter.setMode(Attributes.Mode.Single);

这是适配器:

    public class ViewContactAdapter extends BaseSwipeAdapter {

        Context mContext;
        SwipeLayout rightSwipe, leftSwipe;
        int mSize;
    String Name[], Number[];
    Cursor cursor;
    TextView name,number;

    public ViewContactAdapter(Context context, int size)
    {
        this.mContext=context;
        this.mSize=size;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.view_contact_swipe_layout;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {

        View rootview=LayoutInflater.from(mContext).inflate(R.layout.view_contact_list_adapter,null);
        rightSwipe=rootview.findViewById(getSwipeLayoutResourceId(position));
        leftSwipe=rootview.findViewById(getSwipeLayoutResourceId(position));
        init(rootview);
        return rootview;
    }

    private void init(View rootview) {
        Name=new String[mSize];
        Number=new String[mSize];
        int i=0;
        cursor = rootview.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
        while (cursor.moveToNext())
        {
            Name[i]= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            Number[i] = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            i++;
        }
        name=(TextView) rootview.findViewById(R.id.view_contactlist_name);
        number=(TextView) rootview.findViewById(R.id.view_contactlist_number);
    }

    @Override
    public void fillValues(int position, View convertView) {

        rightSwipe.setShowMode(SwipeLayout.ShowMode.PullOut);
        rightSwipe.addDrag(SwipeLayout.DragEdge.Right,convertView.findViewById(R.id.push_right));

        leftSwipe.setShowMode(SwipeLayout.ShowMode.PullOut);
        leftSwipe.addDrag(SwipeLayout.DragEdge.Left,convertView.findViewById(R.id.push_left));

        name.setText(Name[position]);
        number.setText(Number[position]);

    }

    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

这是完整的logcat错误:

FATAL EXCEPTION: main                                                                           Process: codecharcha.com.contacts, PID: 2503
                                                                        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                            at android.view.ViewGroup.addViewInner(ViewGroup.java:4915)
                                                                            at android.view.ViewGroup.addView(ViewGroup.java:4746)
                                                                            at com.daimajia.swipe.SwipeLayout.addView(SwipeLayout.java:716)
                                                                            at com.daimajia.swipe.SwipeLayout.addDrag(SwipeLayout.java:678)
                                                                            at com.daimajia.swipe.SwipeLayout.addDrag(SwipeLayout.java:659)
                                                                            at codecharcha.com.contacts.Adapter.ViewContactAdapter.fillValues(ViewContactAdapter.java:72)
                                                                            at com.daimajia.swipe.adapters.BaseSwipeAdapter.getView(BaseSwipeAdapter.java:55)
                                                                            at android.widget.AbsListView.obtainView(AbsListView.java:2372)
                                                                            at android.widget.ListView.makeAndAddView(ListView.java:2052)
                                                                            at android.widget.ListView.fillDown(ListView.java:786)
                                                                            at android.widget.ListView.fillGap(ListView.java:750)
                                                                            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5232)
                                                                            at android.widget.ListView.trackMotionScroll(ListView.java:1971)
                                                                            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3581)
                                                                            at android.widget.AbsListView.onTouchMove(AbsListView.java:3990)
                                                                            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3815)
                                                                            at android.view.View.dispatchTouchEvent(View.java:11721)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2955)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
                                                                            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
                                                                            at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
                                                                            at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
                                                                            at android.app.Activity.dispatchTouchEvent(Activity.java:3292)
                                                                            at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
                                                                            at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407)
                                                                            at android.view.View.dispatchPointerEvent(View.java:11960)
                                                                            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4776)
                                                                            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4590)
                                                                            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
                                                                            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
                                                                            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
                                                                            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274)
                                                                            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
                                                                            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4331)
                                                                            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
11-22 21:29:19.337 2503-2503/codecharcha.com.contacts E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
                                                                            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
                                                                            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
                                                                            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
                                                                            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6642)
                                                                            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6616)
                                                                            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6577)
                                                                            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6745)
                                                                            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
                                                                            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
                                                                            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6716)
                                                                            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6768)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:652)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                            at android.os.Handler.handleCallback(Handler.java:789)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

错误异常是:

  

java.lang.IllegalStateException:指定的子级已有父级。您必须先在孩子的父母身上调用removeView()。

请帮忙。感谢

0 个答案:

没有答案