做一个基本的android后台服务应用程序。不太明白为什么会出现错误(MainActivity.java). Error is at btnStart = (Button)findViewById(R.id.btnStart);
提供的快速修复将返回类型设置为“void”。而对于 btnStop 则没有错误。
package com.example.backgroundservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Service;
public class MainActivity extends Activity implements OnClickListener{
btnStart = (Button)findViewById(R.id.btnStart); (ERROR HERE)
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
startService(serviceIntent);
}
});
btnStop.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent (MainActivity.this,MyService.class);
stopService(serviceIntent);
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}