Textview以编程方式创建以拖放Android

时间:2014-12-01 15:23:51

标签: android view drag-and-drop textview

我制作的应用程序允许用户在运行时添加语音气泡并将其放置在屏幕上的任何位置。首先我使用了不同的方法而且失败了。所以我找到了一个简单的拖放方式的教程。 我能够创建一个背景设置为我的9patch图像的文本视图,其中包含用户想要的文本。

这是我的代码:

public class Show extends Activity {
LinearLayout rel;
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
String message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        Bundle bundle = getIntent().getExtras();
        message = bundle.getString("message");
        TextView txt = createText();

        ViewGroup vg = (ViewGroup)findViewById(R.id.rel);
        vg.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getActionMasked())
                {
                    case MotionEvent.ACTION_MOVE:
                        int x = (int)event.getX() - offset_x;
                        int y = (int)event.getY() - offset_y;
                        int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
                        int h = getWindowManager().getDefaultDisplay().getHeight() - 100;

                        if(x > w)
                            x = w;
                        if(y > h)
                            y = h;

                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                        new ViewGroup.MarginLayoutParams(
                                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                                        LinearLayout.LayoutParams.WRAP_CONTENT));
                        lp.setMargins(x, y, 0, 0);
                        selected_item.setLayoutParams(lp);
                        break;
                    default:
                        break;
                }
                return true;
            }
   });


        txt.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                switch(event.getActionMasked())
                                {
                                        case MotionEvent.ACTION_DOWN:
                                                offset_x = (int)event.getX();
                                                offset_y = (int)event.getY();
                                                selected_item = v;
                                                break;
                                        default:
                                                break;
                                }

                                return false;
                        }
                });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show, menu);
        return true;
    }

    public TextView createText(){
        AddNewText adnew = new AddNewText();
        rel = (LinearLayout)findViewById(R.id.rel);
        return adnew.makeText(message, this, rel);
    }
}
}

我知道问题所在,但我不知道如何解决这个小问题。如果我使用" TextView"并通过" findviewbyid"来调用它。拖拉。但是,由于我在运行时创建了一个textview,我想它并没有把它拿起来。

这是我的LogCat:

12-01 15:19:50.944: E/AndroidRuntime(947): FATAL EXCEPTION: main
12-01 15:19:50.944: E/AndroidRuntime(947): java.lang.NullPointerException
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.example.omg.Show$1.onTouch(Show.java:58)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.View.dispatchTouchEvent(View.java:7241)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.View.dispatchPointerEvent(View.java:7426)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.Choreographer.doFrame(Choreographer.java:530)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.os.Handler.handleCallback(Handler.java:725)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.os.Looper.loop(Looper.java:137)
12-01 15:19:50.944: E/AndroidRuntime(947):  at android.app.ActivityThread.main(ActivityThread.java:5041)
12-01 15:19:50.944: E/AndroidRuntime(947):  at java.lang.reflect.Method.invokeNative(Native Method)
12-01 15:19:50.944: E/AndroidRuntime(947):  at java.lang.reflect.Method.invoke(Method.java:511)
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 15:19:50.944: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 15:19:50.944: E/AndroidRuntime(947):  at dalvik.system.NativeStart.main(Native Method)
12-01 15:19:52.784: E/Trace(972): error opening trace file: No such file or directory (2)

它在第58行给出了空指针异常,即 selected_item = v; ,因为它没有将我的TextView保留为所选项目。

0 个答案:

没有答案