Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drag and drop in android 3.x causes illegalStateException after small number on drags

there's a problem with drag and drop mechanism at android 3.x : after doing some drags (say 30 drags) an exception accrues (see the attached link)

https://groups.google.com/forum/#!msg/android-platform/2APvO248NNY/rKI-5dCT8XcJ (I'm getting in log the same thing as attached to that post..)

android technician answers there that it's bug in the API, and says the only way to avoid the problem is to call Garbage Collector.

I did it. the exception not been thrown anymore, but after a while (say more 30-40 drags) android stops calling the drop event from some reason.

I tried to "refresh" all view by release all resources/canvas/drawing cache/recycling bitmaps and re-create them and it didn't helps (didn't throw the exception anymore - but still after some drags the drop event don't work)

the only thing that "helps" is close the activity and restart it again.

anyone solved this problem somehow, or have a good simple alternative??? (beside implement my own drag and drop functionality..)

I would like to get solution that won't force me to restart or re-create anything that don't suppose to..

here is sample code that demonstrate the bug (not demonstrates the part which I said about the problem with the drop event after using the System.GC) :

public class DragandDropExampleActivity extends Activity {

private boolean mIsBeenDragged = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ImageView imageViewToDRag = (ImageView) findViewById(R.id.image_view_to_drag);

    imageViewToDRag.setClickable(true);

    imageViewToDRag.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mIsBeenDragged = true;
                DragShadowBuilder shadowBuilder = new DragShadowBuilder(imageViewToDRag);
                imageViewToDRag.startDrag(null, shadowBuilder, imageViewToDRag, 0);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {

                mIsBeenDragged = false; 
            }
            return false;
        }
    });

}
}

this is the xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image_view_to_drag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >

</ImageView>

this is the stack trace:

06-04 13:34:32.730: E/View(8061):
java.lang.IllegalArgumentException
    at android.view.Surface.lockCanvasNative(Native Method)
    at android.view.Surface.lockCanvas(Surface.java:350)
    at android.view.View.startDrag(View.java:11489)
    at com.show.dragandrop.DragandDropExampleActivity$1.onTouch(DragandDropExampleActivity.java:32)
    at android.view.View.dispatchTouchEvent(View.java:4617)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java: 1862)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1286)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2315)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1835)
    at android.view.View.dispatchPointerEvent(View.java:4689)
    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2415)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:2077)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4126)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    at dalvik.system.NativeStart.main(Native Method)

to make the exception accrue - just drag the image to some point on the screen, and leave the finger. repeat that exactly 30 times, and the exception is thrown. I made this very simple example, to demonstrate that the exception thrown without any overhead caused by my application.

TIA

like image 704
Tal Kanel Avatar asked Mar 05 '12 13:03

Tal Kanel


People also ask

Does Android have drag and drop?

The Android drag and drop framework enables you to add interactive drag and drop capabilities to your app. With drag and drop, users can copy or move text, images, objects—any content that can be represented by a URI—from one View to another within an app or, in multi-window mode, between apps.

How do I drag and drop on Android tablet?

A double tap and hold allows you to grab and then drag. A two-finger tap acts as a right click. A two-finger drag reproduces the mouse wheel scrolling action.


1 Answers

Try this: -

private OnTouchListener drag = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            setViewPosition(v, Math.round(event.getRawX()),
                    Math.round(event.getRawY()));
        }
        return false;
    }
};


private void setViewPosition(View v, int x, int y) {
    if (v != null) {
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.leftMargin = (x - v.getMeasuredWidth() / 2);
        params.topMargin = (y - v.getMeasuredHeight() / 2);
        v.setLayoutParams(params);
        v.invalidate();
    }
}
like image 157
Talha Avatar answered Oct 13 '22 18:10

Talha