首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

写Service Demo的时候遇到的一个有关问题小结一上

2012-08-25 
写Service Demo的时候遇到的一个问题小结一下我写了一个? Service 的Demo,但是将启动Service的Intent声明

写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
讲得比我这个深入

热点排行