• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Move chathead service with button

tsue

Lurker
hello, im having trouble moving a chathead with a button, no matter how much i try it always crashes my app
here is the main activity



Code:
package example.com.chatheads;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.sip.SipAudioCall;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Service;

import example.com.chatheads.ChatHeadService.*;

public class MainActivity extends Activity {
    private final int xx = 500;
    private final int yy = 500;

    Button startService,stopService, moveService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService=(Button)findViewById(R.id.startService);
        stopService=(Button)findViewById(R.id.stopService);
        moveService=(Button)findViewById(R.id.moveService);

        startService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService(new Intent(getApplication(), ChatHeadService.class));
                //startService(new Intent(getApplication(), HepUstte.class));

            }
        });
        stopService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(getApplication(), ChatHeadService.class));

            }
        });

        moveService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //System.out.println("Hello");

                ChatHeadService  inst = new ChatHeadService();
                inst.UpdateEmployee();

            }
        });

    }

and here is the chatheadservice class

Code:
package example.com.chatheads;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;


import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class ChatHeadService extends Service {
    public WindowManager windowManager;
    public ImageView chatHead;
    WindowManager.LayoutParams params;

    int xi ;
    int yi ;

    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("Hello2");

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.face1);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        chatHead.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
              params.x = xi;
              params.y = yi;

              windowManager.updateViewLayout(chatHead, params);
              }

        });
        windowManager.addView(chatHead, params);

    }

    public void MoveHead(){

        System.out.println("Hello");

        WindowManager.LayoutParams params = (WindowManager.LayoutParams) chatHead.getLayoutParams();

        params.x = 100;
        params.y = 300;

        windowManager.updateViewLayout(chatHead, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (chatHead != null)
            windowManager.removeView(chatHead);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
 
Please show the stack trace from your Logcat view.

Code:
04-21 12:04:52.148 17396-17396/example.com.chatheads W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
04-21 12:04:54.718 17396-17396/example.com.chatheads D/ViewRootImpl: ViewPostImeInputStage processPointer 0
04-21 12:04:54.888 17396-17396/example.com.chatheads D/ViewRootImpl: ViewPostImeInputStage processPointer 1
04-21 12:04:54.888 17396-17396/example.com.chatheads I/System.out: Hello
04-21 12:04:54.888 17396-17396/example.com.chatheads D/AndroidRuntime: Shutting down VM
04-21 12:04:54.898 17396-17396/example.com.chatheads E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: example.com.chatheads, PID: 17396
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
                                                                           at example.com.chatheads.ChatHeadService.MoveHead(ChatHeadService.java:153)
                                                                           at example.com.chatheads.MainActivity$3.onClick(MainActivity.java:71)
                                                                           at android.view.View.performClick(View.java)
                                                                           at android.widget.TextView.performClick(TextView.java)
                                                                           at android.view.View$PerformClick.run(View.java)
                                                                           at android.os.Handler.handleCallback(Handler.java)
                                                                           at android.os.Handler.dispatchMessage(Handler.java)
                                                                           at android.os.Looper.loop(Looper.java)
                                                                           at android.app.ActivityThread.main(ActivityThread.java)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
 
The code does not correspond to the stack trace. Could you please provide the code which was in use when the stack trace was generated. Thank you.
 
The code does not correspond to the stack trace. Could you please provide the code which was in use when the stack trace was generated. Thank you.

thats weird ill upload it again, and ill upload the full file

Code:
package example.com.chatheads;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.sip.SipAudioCall;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Service;

import example.com.chatheads.ChatHeadService.*;

public class MainActivity extends Activity {
    private final int xx = 500;
    private final int yy = 500;

    Button startService,stopService, moveService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService=(Button)findViewById(R.id.startService);
        stopService=(Button)findViewById(R.id.stopService);
        moveService=(Button)findViewById(R.id.moveService);

        startService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService(new Intent(getApplication(), ChatHeadService.class));
                //startService(new Intent(getApplication(), HepUstte.class));

            }
        });
        stopService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(getApplication(), ChatHeadService.class));

            }
        });

        moveService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                //System.out.println("Hello");

                TextView textElement = (TextView) findViewById(R.id.counter);
                textElement.setText("I love you");


                //startService(new Intent(getApplication(), MoveHeadService.class));

                /*ChatHeadService  inst = new ChatHeadService();
                inst.ChatHeadService(xx, yy);*/

                ChatHeadService  inst = new ChatHeadService();
                inst.MoveHead();

                //Intent it = new Intent(getApplication(), ChatHeadService.class);
                //it.UpdateEmployee();



            }
        });

    }




    /*ChatHeadService  inst = new ChatHeadService();
                inst.UpdateEmployee();*/


      //  public void precionarBoton(OnClickListener al){
        //ChatHeadService x = new  ChatHeadService();

    //}
        /*final Button button = (Button) findViewById(R.id.moveService);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                view.setOnClickListener(new ExternalOnClickListener());
                // Perform action on click
            }
        });*/

        /*moveService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(getApplication(), ChatHeadService.class));

            }
        });*/


    }

chathead service

Code:
package example.com.chatheads;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;


import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class ChatHeadService extends Service {
    public WindowManager windowManager;
    public ImageView chatHead;
    WindowManager.LayoutParams params;


    int xi ;
    int yi ;

    /*public void parametros (int x, int y){
        xi = x;
        yi = y;
    }*/

    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("Hello2");

        //setContentView(R.layout.content_layout_id);
        //setContentView(R.layout.activity_main);
        //moveService=(Button)findViewById(R.id.moveService);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.face1);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);



        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;



        chatHead.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
              params.x = xi;
              params.y = yi;

              windowManager.updateViewLayout(chatHead, params);
              }

        });


        //this code is for dragging the chat head
        /*chatHead.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX
                                + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY
                                + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(chatHead, params);
                        return true;
                }
                return false;
            }


        });*/

        windowManager.addView(chatHead, params);




        /*class Box extends View {
            private Paint paint = new Paint();
            Box(Context context) {
                super(context);
            }


            public void UpdateEmployee() {
                //super.onCreate();

                System.out.println("Hello");

                params.x = 0;
                params.y = 100;

                windowManager.updateViewLayout(chatHead, params);

                windowManager.updateViewLayout(chatHead, params);
            }
        }*/


    }



    public void MoveHead(){
        //super.onCreate();

        System.out.println("Hello");

        WindowManager.LayoutParams params = (WindowManager.LayoutParams) chatHead.getLayoutParams();

        params.x = 100;
        params.y = 300;

        windowManager.updateViewLayout(chatHead, params);
    }







    /*public class ExternalOnClickListener implements View.OnClickListener {

        public ExternalOnClickListener(...) {
            // keep references for your onClick logic
        }

        @Override public void onClick(View v) {
            // TODO: add code here
        }

    }*/

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (chatHead != null)
            windowManager.removeView(chatHead);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
 

Attachments

The code does not correspond to the stack trace. Could you please provide the code which was in use when the stack trace was generated. Thank you.

i have been still searching for this solution but cant find anything, i im thinking maybe its not possible :S
 
Do you understand what a NullPointerException is?

The problem is happening because you manually create a new instance of your ChatHeadService, and the member variable chatHead isn't initialised properly - it remains null when you invoke the MoveHead() method.

Code:
                ChatHeadService  inst = new ChatHeadService();
                inst.MoveHead();

The question is, why are you creating a new ChatHeadService? If you want to start a service, there is a proper way of doing this

https://developer.android.com/guide/components/services.html
 
Back
Top Bottom