大哥 你这报错信息呢?
FATAL EXCEPTION: main
后面的信息呢?  没有的话没法解决呀
还有下面这段代码能编译通过吗?
if (togglebutton.isChecked()) {
            intent = new Intent();
            intent.setAction("com.example.temp.MY_SERVICE");
            startService(intent);                
        } else {
            stopService(intent);
        }

解决方案 »

  1.   

    intent = new Intent();初始化Intent的时候,将当前activity的上下文传过去,如:
    Intent intent = new Intent(yourActivity.this,yourService.class);
      

  2.   

    能,编译没问题,就是按togglebutton后报错退出了。
    错误信息如下
      

  3.   

    这个startService我查了很多资料发现了有两种方法,但不知道有什么区别,你能告诉我这两种的区别吗?方法一:
    intent = new Intent();
    intent.setAction("com.example.temp.MY_SERVICE");
    startService(intent);方法二:
    Intent intent = new Intent(MainActivity.this, MyService.class);
    startService(intent);
      

  4.   

    无法实例化一个Service对象,原因是com.example.temp.Service类声明没找到。你看下你的服务名称是不是写错了
      

  5.   

    请问这个是在哪里声明的?
    AndroidManifest.xml
            <service android:name="MyService">
             <intent-filter >
             <action android:name="com.example.temp.MY_SERVICE"/>
             </intent-filter>
            </service>MyService.java
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.Toast;
    public class MyService extends Service {MainActivity.java
    package com.example.temp;import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ToggleButton;public class MainActivity extends Activity {
    ... ...
    intent = new Intent();
    intent.setAction("com.example.temp.MY_SERVICE");
    startService(intent);