Hi everyone!

I am trying to start a service an unusual way. The reason for this is
to keep at least some parts of the program platform independent. The
startService() function is called from the Service class itself and
there
I get a NullPointerException.

I made a small example to show the problem (see below). Any ideas?
Thanks!


/* HelloAndroid.java */
package com.example.helloandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

package com.example.helloandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

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

        // start buttons
        findViewById(R.id.start).setOnClickListener(new
OnClickListener() {
            public void onClick(View v) {
                startService(new
Intent("com.example.helloandroid.UnusualService"));
            }
        });
        // stop buttons
        findViewById(R.id.stop).setOnClickListener(new
OnClickListener() {
            public void onClick(View v) {
                stopService(new
Intent("com.example.helloandroid.UnusualService"));
            }
        });
        // unusual start
        findViewById(R.id.unusual).setOnClickListener(new
OnClickListener() {
            public void onClick(View v) {
                UnusualService myService = new UnusualService();
                myService.startService();
            }
        });
    }
}

/* UnusualService */
package com.example.helloandroid;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class UnusualService extends Service {
        @Override
        public IBinder onBind(Intent intent) {
                return null;    // TODO make something
        }

        public void startService() {
                // the folowing line will throw the exception
                startService(new 
Intent("com.example.helloandroid.UnusualService"));
        }

        public void stopService() {
                stopSelf();
        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to