2

Hi I am currently learning how to use service and notifications in android and I am facing a NullPointerException when I am trying to start a service. I wrote a small app where I have a button and 2 EditTextes (title and content). When I click on the button, I retrieve the values in the EditTextes and I start a new service that creates a notification. My code is splitted in two parts. Here is my code. This one presents the main activity of the application :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.serviceButton);

    final Context ctx = getApplicationContext();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent i = new Intent(ctx, NotificationService.class);


            Toast.makeText(ctx, "Clicking ...", Toast.LENGTH_SHORT).show();

            EditText title = (EditText) findViewById(R.id.notif_title);
            EditText content = (EditText) findViewById(R.id.notif_content);

            Bundle bundle = new Bundle();
            bundle.putCharSequence("NOTIF_TITLE", title.getText());
            bundle.putCharSequence("NOTIF_CONTENT", content.getText());
            i.putExtras(bundle);

            startService(i);

        }
    });
}

This one describes the service that will launch the notification :

public class NotificationService extends IntentService {

public NotificationService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    Context ctx = getApplicationContext();
    Toast.makeText(ctx, "NotificationService", Toast.LENGTH_LONG).show();

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);


    Bundle b = intent.getExtras();

    String title = b.getString("NOTIF_TITLE");
    String content = b.getString("NOTIF_CONTENT");
    Intent i = new Intent(this, NotificationService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i,
            Intent.FLAG_ACTIVITY_NEW_TASK);

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon,
            "New notification arrives", when);

    notification.setLatestEventInfo(ctx, title, content, pendingIntent);

    notificationManager.notify(
            NOTIFICATIONS.HELLO_NOTIFICATION.ordinal(), notification);
}

}

When I click on the button, I have this exception :

    FATAL EXCEPTION: main
 java.lang.NullPointerException 
at  android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:67)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:62)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Edit: I made correction to my code, removing new NotificationService but I still get a NullPointerException

Dimitri
  • 8,122
  • 19
  • 71
  • 128

3 Answers3

6

You dont run your service by initiating its object as new. Simply run your service as below:

startService(i);

For more info, read this

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

I finally correct my problem. After I made the correction suggested by Terrance, @waqas, @akki, I had another exception : InstantiationException. The problem was in my NotificationService class. When you extend the IntentService to define a new service, you must define a default constructor but this constructor does not take parameters and we must call a super(name) inside it.
I just removed the parameter in the constructor in the NotificationService class and everything works fine. Thanks for your help !!

Dimitri
  • 8,122
  • 19
  • 71
  • 128
0

I think akki is correct http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/ so just remove the instantiation of your NotificationService. And just call make your intent as seen in step three of the link.

So Something like this


button.setOnClickListener(new View.OnClickListener() {      
        @Override      
        public void onClick(View v) {      
            startService(i);

            Toast.makeText(ctx, "Clicking ...", Toast.LENGTH_SHORT).show();      

            EditText title = (EditText) findViewById(R.id.notif_title);      
            EditText content = (EditText) findViewById(R.id.notif_content);      

            Bundle bundle = new Bundle();      
            bundle.putCharSequence("NOTIF_TITLE", title.getText());      
            bundle.putCharSequence("NOTIF_CONTENT", content.getText());      
            i.putExtras(bundle);      

            notificationService.startService(i);      

        }      
    });      
Terrance
  • 11,764
  • 4
  • 54
  • 80