从服务类本身startService() [英] startService() from the service class itself

查看:92
本文介绍了从服务类本身startService()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着开始从服务的类的机器人服务。
之所以这样做是为了实现某些平台的独立性。

这样做我得到在android.content.ContextWrapper.startService(ContextWrapper.java:326)一个NullPointerException异常。该平台的目标是2.1 UPDATE1,有什么建​​议?

查看code以下(我离开了进口,以节省一些空间)

  / * GUI:HelloAndroid.java * /
包com.example.helloandroid;公共类HelloAndroid延伸活动{
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        // 这样可行!
        startService(新意图(com.example.helloandroid.UnusualService));        //停止当然服务的工作原理,也
        stopService(新意图(com.example.helloandroid.UnusualService));        //不寻常的开始不工作:
        UnusualService为myService =新UnusualService();
        myService.startService();
    }
}/ *服务:UnusualService.java * /
包com.example.helloandroid;公共类UnusualService延伸服务{
    @覆盖
    公共无效的onCreate(){
        Toast.makeText(这一点,R.string.service_started,Toast.LENGTH_SHORT).show();
    }    @覆盖
    公共无效的onDestroy(){
        Toast.makeText(这一点,R.string.service_stopped,Toast.LENGTH_SHORT).show();
    }    @覆盖
    公众的IBinder onBind(意向意图){
        返回null; //使这里的东西,其余的作品时,
    }    公共无效startService(){
        //如下因素线将导致NullPointerException异常
        startService(新意图(com.example.helloandroid.UnusualService));
    }    公共无效stopService(){
        stopSelf();
    }
}


解决方案

当然 - 新创建的服务没有到上下文的引用,所以上下文是因此,该系统将引发 NullPointerException异常
请记住:不要用自己创建一个服务 - 系统为您完成此

I try to start an android service from the service's class. The reason to do this is to achieve some platform independence.

Doing this I get a NullPointerException at android.content.ContextWrapper.startService(ContextWrapper.java:326). The platform target is 2.1-update1, any suggestions?

See the code below (I left the imports out to save some space)

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

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

        // that works!
        startService(new Intent("com.example.helloandroid.UnusualService"));

        // stop the service works of course, too
        stopService(new Intent("com.example.helloandroid.UnusualService"));

        // unusual start does not work:
        UnusualService myService = new UnusualService();
        myService.startService();
    }
}

/* Service: UnusualService.java */
package com.example.helloandroid;

public class UnusualService extends Service {    
    @Override
    public void onCreate() {
        Toast.makeText(this, R.string.service_started, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, R.string.service_stopped, Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null; // make something here when the rest works 
    }

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

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

解决方案

Of course - your newly created service does not have a reference to a context, so the context is null and therefore the system throws a NullPointerException. Remember: Do not create a service on your own by using new - the system does this for you!

这篇关于从服务类本身startService()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆