拖动但空的clipdata

时间:2012-06-24 14:01:33

标签: android drag-and-drop

我找不到问题所在,我认为我把东西放进了项目并且剪辑数据正确, 但是我的onDrag总是收到一个空指针异常,甚至无法获得项目数。

这是我的OnDragListener类

    public class ScaleImageSize implements View.OnDragListener{

            @Override
            public boolean onDrag(View arg0, DragEvent arg1) {
                // TODO Auto-generated method stub
                final int action = arg1.getAction();                    
                Integer tag  = (Integer) arg0.getTag();
                Log.d("tag",Integer.toString(tag));

                int count = arg1.getClipData().getItemCount();
                Log.d("count",Integer.toString(count));


                ClipData.Item item = arg1.getClipData().getItemAt(0);
                String tagString = item.getText().toString();
                int tagOfType = Integer.valueOf(tagString);

                switch(action){

                case DragEvent.ACTION_DRAG_STARTED:

                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    augImg( tag, (int) arg1.getY());

                    augImg( tagOfType, (int)arg1.getY());

                    return true;
                case DragEvent.ACTION_DROP:

                    return true;
                }               
                return false;
            }

        };

这是我的OnLonClickListener类,它应该调用函数startdrag。

       OnLongClickListener StartScaleImageSize = new OnLongClickListener(){

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            int i  =  (Integer) v.getTag();
            beingLongClick[i] = true;

            ClipData.Item item = new ClipData.Item((CharSequence)Integer.toString(i));
            String[] clipDescription = {ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData = new ClipData((CharSequence)Integer.toString(i), 
                    clipDescription,
                    item);
            dragData.addItem(item);

            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(dragData, shadowBuilder, v, 0);
            return false;
        }
    };

错误消息

06-24 21:47:21.205: E/AndroidRuntime(4123): FATAL EXCEPTION: main
06-24 21:47:21.205: E/AndroidRuntime(4123): java.lang.NullPointerException
06-24 21:47:21.205: E/AndroidRuntime(4123):     at traveler.com.TravelerpActivity$TypeMgr$ScaleImageSize.onDrag(TravelerpActivity.java:237)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.View.dispatchDragEvent(View.java:13619)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.os.Looper.loop(Looper.java:137)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at java.lang.reflect.Method.invoke(Method.java:511)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-24 21:47:21.205: E/AndroidRuntime(4123):     at dalvik.system.NativeStart.main(Native Method)
不过,任何建议都会很棒!

2 个答案:

答案 0 :(得分:2)

尝试以这种方式创建dragData:

ClipData dragData = ClipData.newPlainText("simple text", toString(i));

答案 1 :(得分:1)

我知道这是一个超旧的线程,但是最近遇到了这个问题。显然,clipData仅适用于拖放事件(DragEvent.ACTION_DROP),而不适用于拖动事件。官方文件中还不清楚。