背景中的服务活动崩溃

时间:2016-04-06 21:02:19

标签: java android xml android-studio service

我正在开发一个简单的服务应用程序,它会使计数器递增1秒,并在每10秒后显示一次通知。我已经使用过线程,所以当我关闭我的应用服务时会在后台运行。

问题 - 一切正常,但是当按下按钮后启动计数器后我立即按下后退按钮。我的应用程序崩溃了。有人能告诉我这是什么问题吗?谢谢!

public class MyService extends Service {
        private static final int NOTIFICATION_ID = 5453;
        String s;
        boolean flag=true;
        int cooo;
        int m;
        int n=0;

       private void triggerNotification(final int text){   //Notification Triggering Function
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_noti);
                Intent intent = new Intent(this, ServiceActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
                stackBuilder.addParentStack(ServiceActivity.class);   // Creates a virtual stack so that when back key is pressed main activity will be displayed
                stackBuilder.addNextIntent(intent);
                PendingIntent pendingIntent =
                        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                        );
                Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                CharSequence tickerText="Current Counter Value :"+text;
                Notification notification = new Notification.Builder(this)
                        .setLargeIcon(bm)
                        .setSmallIcon(R.drawable.ic_action_noti)             // Setting Notification Icon
                        .setContentTitle(getString(R.string.notification))                // Setting Title of Notification
                        .setAutoCancel(true)                               // Makes this notification automatically dismissed when the user touches it
                        .setPriority(Notification.PRIORITY_MAX)           // Setting priority to Max
                        .setContentIntent(pendingIntent)
                        .setContentText(tickerText)
                        .setLights(0xFF0000FF, 100,3000)
                        .setSound(soundUri)
                        .build();

                notification.defaults |= Notification.DEFAULT_VIBRATE;
                notification.defaults |= Notification.DEFAULT_SOUND;

                NotificationManager notificationManager =                        //Notification Manager
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(NOTIFICATION_ID, notification);
                // Log.d(TAG, "Notification sent successfully.");

            }
        @Nullable
        @Override


        public IBinder onBind(Intent intent) {
            return null;
        } //Default function of service class

        public int onStartCommand(Intent intent, int flags, int startID){ //Function recieving intent from startService

            cooo=intent.getIntExtra("cont", 0);
            s=Integer.toString(cooo);
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() { //Thread Initialized
                    while(flag){
                       try{ Log.d("The counter value is:", s + " ");
                        m=Integer.parseInt(s);
                        m++;
                           n++;
                        s=Integer.toString(m);

                           if(n%10==0){
                               Log.d("Notification Triggered", "Congrats!");
                               triggerNotification(m);
                           }

                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            });
            thread.start(); //Thread Started
            return super.onStartCommand(intent,flags,startID); //sending intent again
        }

        public void onDestroy(){ //function call on stop button pressed
            super.onDestroy();
            flag=false;
            Log.d("Notification Triggered","on stop Button");
            triggerNotification(m);
            //Toast.makeText(getApplicationContext(),"stopped",Toast.LENGTH_LONG).show();
        }
    }
  

错误日志 -

     

04-06 23:06:11.450 16940-16940 / headfirst.com.serviceapplication   E / AndroidRuntime:致命异常:主要                                                                                     java.lang.RuntimeException:无法启动服务   headfirst.com.serviceapplication.MyService@427724e8 with null:   显示java.lang.NullPointerException                                                                                         在   android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)                                                                                         在android.app.ActivityThread.access $ 2000(ActivityThread.java:159)                                                                                         在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1419)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                         在android.os.Looper.loop(Looper.java:176)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5419)                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:525)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)                                                                                         at dalvik.system.NativeStart.main(Native Method)                                                                                      引起:java.lang.NullPointerException                                                                                         在   headfirst.com.serviceapplication.MyService.onStartCommand(MyService.java:72)                                                                                         在   android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2833)                                                                                         在android.app.ActivityThread.access $ 2000(ActivityThread.java:159)                                                                                         在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1419)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                         在android.os.Looper.loop(Looper.java:176)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5419)                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:525)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)                                                                                         在dalvik.system.NativeStart.main(本地方法)

0 个答案:

没有答案