Question

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

Was it helpful?

Solution 5

a lot of time passed since I asked that question. when android ICS was released - this bug was not exists any more. so since android ICS there is really no reason writing applications targeted to Honeycomb. anyway only 0.3% of total android devices running this version of OS,

so for me the answer is - just not to use Honeycomb! :->

OTHER TIPS

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();
    }
}

If restarting the activity is helping so try restarting the view, by removing it and adding it again(may be new instance). Also you can try the regular staff(That usually doesn't help) like invalidate(), override the onDraw() and see why it is called or not called.

Try this:

imageViewToDRag.startDrag(null, shadowBuilder, imageViewToDRag, 0);
System.gc();

It worked for me on Android 3.2!

From a comment on the question covering the problem you posted to the Google Groups discussion about:

This problem occurs because GC not perform collecting of drags. But if window redrew, GC perform collecting of drags. Window will be redrawn after soft keyboard opened or some ImageView updated or something like this.

So it sounds like you can get the garbage collector to collect the drag objects by forcing a redraw of the containing View. I don't have a 3.0 device handy, but perhaps something like this would be worth a try:

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
           findViewById(R.id.main_frame).invalidate();
        }

You might need the suggested garbage collection hint in there as well:

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
           findViewById(R.id.main_frame).invalidate();
           System.gc();
        }

but I think you're right to want to avoid that if possible, so I'd only try that if it doesn't work without it.

Also, I think onTouch() should return true, since you've consumed the touch event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top