Error when using a Sevice, when the Activity that I call it is closed

0

I have a service that runs fine, until I close the Activity that called it or destroyed it in this case: Activity_Calls. In the service I call this method:

 public void TactivarBroucast(int a,int b) {
        int t=b*3600000+a*1000; 
        Date date = new Date(System.currentTimeMillis());
       Calendar c = Calendar.getInstance();
       c.setTime(date);
       c.set(Calendar.MILLISECOND,t);
       date = c.getTime();
       Timer time = new Timer();
       time.schedule(new Temporisador(), date);
   }

Already in the class in charge of the Service I create an object of the class Activity_Calls:

Activity_llamadas activity_llamadas;

in onCreate() and onStartCommand() methods:

public void onCreate(){
    activity_llamadas= new Activity_llamadas();
    super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) { 
    Activity_llamadas.getmInstanceActivity().TactivarBroucast(10,0);//valores de a y b. Linea 41 del segundo Logcat 
 return Service.START_STICKY;
} //linea 43 del primer logcat

The methods onDestroy() and onStart() only I have declared have nothing inside. I find it funny that I put the classic "The application has stopped" 2 times and I generated two identical Logcat (for me): First Logcat:

03-09 17:01:26.085 14276-14276/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.andry.myapplication, PID: 14276
                                                                             java.lang.RuntimeException: Unable to start service com.example.andry.myapplication.MyService@41dcbad0 with null: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2885)
                                                                                 at android.app.ActivityThread.access$2100(ActivityThread.java:151)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                 at android.os.Looper.loop(Looper.java:193)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at com.example.andry.myapplication.MyService.onStartCommand(MyService.java:43)
                                                                                 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2868)
                                                                                 at android.app.ActivityThread.access$2100(ActivityThread.java:151) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                 at android.os.Looper.loop(Looper.java:193) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5299)
 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 

Second Logcat:

03-09 17:28:32.980 14451-14451/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.andry.myapplication, PID: 14451
                                                                             java.lang.RuntimeException: Unable to start service com.example.andry.myapplication.MyService@41dceac0 with null: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2885)
                                                                                 at android.app.ActivityThread.access$2100(ActivityThread.java:151)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                 at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
at com.example.andry.myapplication.MyService.onStartCommand(MyService.java:41)
                                                                                 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2868)
                                                                                 at android.app.ActivityThread.access$2100(ActivityThread.java:151) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                 at android.os.Looper.loop(Looper.java:193) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5299)
 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 

Say again that it works well, with the phone screen off, with the activity in the background, but when closing the app, error. Thanks in advance

    
asked by Andry_UCI 09.03.2018 в 23:39
source

1 answer

0

Initialize the class and call the method within onStartCommand() :

public int onStartCommand(Intent intent, int flags, int startId) { 

    activity_llamadas = new Activity_llamadas();
    activity_llamadas.TactivarBroucast(10,0);//valores de a y b. Linea 41 del segundo Logcat 

 return Service.START_STICKY;
} 
    
answered by 10.03.2018 в 01:13