按钮onClick中的android服务语法错误

按钮onClick中的android服务语法错误,android,button,service,onclick,Android,Button,Service,Onclick,服务未启动。它在按钮onclick下给我一个语法错误,以在startService(intService)启动服务;看起来服务的声明是正确的,但它要求我在onclick按钮下将服务声明为变量 public class Ship extends Activity implements View.OnClickListener { private static final String TAG = "ShipService"; public static final Integer[] TI

服务未启动。它在按钮onclick下给我一个语法错误,以在startService(intService)启动服务;看起来服务的声明是正确的,但它要求我在onclick按钮下将服务声明为变量

    public class Ship extends Activity implements View.OnClickListener {
private static final String TAG = "ShipService";
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Button stop1;
public Spinner spinner2;

// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    stop1 = (Button) findViewById(R.id.stop);
    stop1.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
    android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    Intent intService =  new Intent(Ship.this, Shipservice.class);
    intService.putExtra("TIME_IN_MINUTES", 1000);
    startService(intService);

}

// Handle button callback
@Override
public void onClick(View v) {
          Intent intService =  new Intent(Ship.this, Shipservice.class);
    switch (v.getId()) {
    case R.id.btn2:
        Log.d(TAG, "onClick: starting service");
        startService(intService);
        break;
    case R.id.stop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(new Intent(this, Shipservice.class));
        break;
    }
}
}
public class Ship extends活动实现View.OnClickListener{
私有静态最终字符串TAG=“ShipService”;
公共静态最终整数[]时间(单位:分钟)={30,45,60,180,360};
公共媒体播放器;
public Handler=new Handler();
公共按钮2;
公共按钮停止1;
公共纺纱机纺纱机2;
//初始化活动
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ship);
button2=(按钮)findViewById(R.id.btn2);
按钮2.setOnClickListener(此);
stop1=(按钮)findViewById(R.id.stop);
stop1.setOnClickListener(此);
喷丝头2=(喷丝头)findViewById(R.id.spinner2);
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项目,时间(单位:分钟);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头2.设置适配器(适配器);
Intent intService=新Intent(Ship.this,Shipservice.class);
intService.putExtra(“时间单位:分钟”,1000);
startService(intService);
}
//句柄按钮回调
@凌驾
公共void onClick(视图v){
Intent intService=新Intent(Ship.this,Shipservice.class);
开关(v.getId()){
案例R.id.btn2:
Log.d(标记“onClick:启动服务”);
startService(intService);
打破
案例R.id.stop:
Log.d(标记“onClick:stoppingsrvice”);
stopService(新意图(这个,Shipservice.class));
打破
}
}
}

onCreate
中声明并初始化
意图
变量。它是
onCraete
方法的局部变量。您应该将变量声明为类成员。

您可以发布完整的代码吗。还将
Intent intService
声明为类成员,并在
onCreate
中初始化它,然后调用
startService(intService)单击按钮我有一个服务类,名为shipservice.class,它是我设置intService的值。请重新阅读:
intService
未在
onClick
中定义,仅在
onCreate
中定义。因此添加Intent intService=new Intent(Ship.this,shipservice.class);在onclick下面,switch语句上面,应该修复这个问题吗?我刚刚更新了代码以反映这一点