读书人

写Service Demo的时候遇到的一个有关问

发布时间: 2012-08-25 10:06:20 作者: rapoo

写Service Demo的时候遇到的一个问题小结一下

我写了一个? Service 的Demo,但是将启动Service的Intent声明为主Activity的成员变量时,

?
系统总是在启动的报异常:

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);startSer1 = (Button) findViewById(R.id.startSer1);stopSer1 = (Button) findViewById(R.id.stopSer1);startSer2 = (Button) findViewById(R.id.startSer2);stopSer2 = (Button) findViewById(R.id.stopSer2);log = (TextView) findViewById(R.id.log);startSer1.setOnClickListener(btnListener);stopSer1.setOnClickListener(btnListener);startSer2.setOnClickListener(btnListener);stopSer2.setOnClickListener(btnListener);intent = new Intent(MyServiceActivity.this, MyService.class);}

?

通过这个Demo,吸取到了一个教训:需要使用Context方面的信息,最好在onCreate()方法之后(包括之)。

?

1 楼 Rainbow702 2011-08-09 大家也可以参考这篇BLOG:http://hi.baidu.com/gaogaf/blog/item/7efdb01185806264ca80c4d9.html
讲得比我这个深入

读书人网 >移动开发

热点推荐