momal 0 Newbie Poster

I'm making an app that allows users to add speech bubbles on runtime and place them any where on the screen. First I was using a different approach and I failed. So I found a tutorial that had a simple drag and drop way. I am able to create a textview with background set as my 9patch image with the text that the user wants.

This is my code

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

    }



}
}

I know where the problem is, but I have no idea how to fix the little bugger. If I use a "TextView" and call it by the "findviewbyid" that drags and drops. But since I'm creating a textview on runtime, I guess it's not picking it up

This is my 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)

It's giving a null pointer exception on line 58 and that is selected_item = v; and because it's not keeping my TextView as the selected item.

Can I get some help here, please? I'll be really grateful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.