在Android中拖放视图

时间:2014-12-01 08:21:11

标签: java android drag-and-drop views

我基本上制作了一个漫画风格的应用程序,用户可以在其中为图像添加多个语音气泡。我是新手,我不知道为什么我的应用程序崩溃了。我从这里和那里学到了一些教程,这就是我到目前为止所做的。

我可以在运行时创建一个带有文本的新textview,它具有我的9patch语音气泡的背景。我可以选择该textview并拖动它。但是一旦我放下它,textview就会粘在那里,应用程序崩溃了。另外,当我放下原件时,原件停留在那里。

我正在添加代码以及图像。

 private RelativeLayout mLayout;
       private EditText mEditText;
       private Button mButton;

       private android.widget.RelativeLayout.LayoutParams layoutParams;


       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           mLayout = (RelativeLayout) findViewById(R.id.lay);
           mEditText = (EditText) findViewById(R.id.editTxt);
           mButton = (Button) findViewById(R.id.btnAddNew);
           mButton.setOnClickListener(onClick());
           TextView textView = new TextView(this);
           textView.setText("New text");
           findViewById(R.id.lay).setOnDragListener(dragListener);

       }

//This creates the new textview
       private OnClickListener onClick() {
           return new OnClickListener() {

               @Override
               public void onClick(View v) {
                   mLayout.addView(createNewTextView(mEditText.getText().toString()));
               }
           };
       }

      OnLongClickListener longListener = new OnLongClickListener()
    {
        @Override
        public boolean onLongClick(View v) 
        {
            TextView txtView = (TextView) v;


            View.DragShadowBuilder myShadowBuilder = new DragShadowBuilder(txtView);

            ClipData data = ClipData.newPlainText("", "");
            v.startDrag(data, myShadowBuilder, txtView, 0);

            return true;
        }

    };

        OnDragListener dragListener = new OnDragListener()
        {
            @Override
            public boolean onDrag(View v, DragEvent event) 
            {
                int dragEvent = event.getAction();


                switch(dragEvent)
                {


case DragEvent.ACTION_DRAG_ENTERED:
                        //dropText.setTextColor(Color.GREEN);

                         break;

                    case DragEvent.ACTION_DRAG_STARTED:
                        //dropText.setTextColor(Color.GREEN);
                         layoutParams = (RelativeLayout.LayoutParams) 
                           v.getLayoutParams();
                         break;

                    case DragEvent.ACTION_DRAG_EXITED:

                        break;

                    case DragEvent.ACTION_DROP:


                        layoutParams.leftMargin = (int)event.getX();
                        layoutParams.topMargin = (int)event.getY();
                        v.setLayoutParams(layoutParams);
                        break;
                }

                return true;
            }

        };



       private TextView createNewTextView(String text) {


           final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
           final TextView textView = new TextView(this);
           textView.setLayoutParams(lparams);
           textView.setText("New text: " + text);
           textView.setBackgroundResource(R.drawable.ss);
           textView.setWidth(100);
           ViewGroup.LayoutParams params = textView.getLayoutParams();
           params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
           textView.setLayoutParams(params);
           textView.setOnLongClickListener(longListener);

           return textView;
           }

The main layout

after i enter some text

when i have my mouse dragging it

after i let go

Log Cat错误:

12-01 08:30:04.695: E/AndroidRuntime(5339): FATAL EXCEPTION: main
12-01 08:30:04.695: E/AndroidRuntime(5339): java.lang.NullPointerException
12-01 08:30:04.695: E/AndroidRuntime(5339):     at com.example.adddragtry.MainActivity$2.onDrag(MainActivity.java:115)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.View.dispatchDragEvent(View.java:16371)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1251)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3838)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewRootImpl.access$600(ViewRootImpl.java:95)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2999)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at  android.os.Looper.loop(Looper.java:137)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at  java.lang.reflect.Method.invokeNative(Native Method)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at java.lang.reflect.Method.invoke(Method.java:511)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 08:30:04.695: E/AndroidRuntime(5339):     at  dalvik.system.NativeStart.main(Native Method)

初始化layoutparam后的新Logcat

12-01 10:08:57.116: E/AndroidRuntime(749): FATAL EXCEPTION: main
12-01 10:08:57.116: E/AndroidRuntime(749): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
12-01 10:08:57.116: E/AndroidRuntime(749):  at com.example.adddragtry.MainActivity$2.onDrag(MainActivity.java:89)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.View.dispatchDragEvent(View.java:16371)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1251)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1188)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1200)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1200)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3838)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewRootImpl.access$600(ViewRootImpl.java:95)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2999)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.os.Looper.loop(Looper.java:137)
12-01 10:08:57.116: E/AndroidRuntime(749):  at android.app.ActivityThread.main(ActivityThread.java:5041)
12-01 10:08:57.116: E/AndroidRuntime(749):  at java.lang.reflect.Method.invokeNative(Native Method)
12-01 10:08:57.116: E/AndroidRuntime(749):  at java.lang.reflect.Method.invoke(Method.java:511)
12-01 10:08:57.116: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 10:08:57.116: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 10:08:57.116: E/AndroidRuntime(749):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

layoutParams永远不会被初始化,所以它总是为空。

因此,当您尝试执行此行layoutParams.leftMargin = (int)event.getX();时,您将获得NPE,因为layoutParams为空。如果您想使用视图的布局参数,请尝试使用:

layoutParams = v.getLayoutParams();在您尝试更改其值之前。