Start service with Timertask

Asked

Viewed 129 times

1

I am trying to start an Android notification service with Timertask but returns me the following error:

02-03 16:06:00.164  15051-15379/com.celiogarcia.setapp E/AndroidRuntime﹕ FATAL EXCEPTION: Timer-0
java.lang.NullPointerException
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
        at android.content.ComponentName.<init>(ComponentName.java:75)
        at android.content.Intent.<init>(Intent.java:3845)
        at com.celiogarcia.setapp.Sentado$RingNotification.run(Sentado.java:70)
        at java.util.Timer$TimerImpl.run(Timer.java:284)

Here is my code ... Maybe you can see what is dark to me ...

public class Sentado extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sentado);

               ...
            }
        });
    }

    public class RingNotification extends TimerTask {
            @Override
            public void run() {
                startService(new Intent(Sentado.this, NotificationService.class));
            }
     }


    public void setTimer(){
                Timer timer = new Timer();
                TimerTask ringNotify = new RingNotification();



                timer.scheduleAtFixedRate(ringNotify, 60000, 60000);
    }
}
  • Put all the generated stacktrace to facilitate the identification of the problem.

  • @Andréribeiro made...

  • I believe the error is in "context" but the truth is that I see no other way to pass the parameter ...

  • Is this an Android app? Why are you using the method public static void main?

  • Yes. In this example I used this method to simplify the explanation of my problem, by background, the result you give in this example, is even that I will have in my project...

  • What is the class Sentado?

  • It’s the class Mother....

  • But what is your type? It’s Activity or other?

  • É uma Activity ...

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.