其实就是《深入浅出》中的一个例子,关于service生命周期的。我就是按照书上的代码写的,如下:
public class MyTestActivity extends Activity {
private Button start;
private Button stop;
private Button bind;
private Button unbind;
private ServiceConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.startService);
stop=(Button)findViewById(R.id.stopService);
bind=(Button)findViewById(R.id.bindService);
unbind=(Button)findViewById(R.id.unbindService);
connection=new ServiceConnection(){ @Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
} @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
OnClickListener l=new OnClickListener(){
@Override
public void onClick(View v) {
Intent i=new Intent(MyTestActivity.this,MyService.class);
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startService:
startService(i);
break;
case R.id.stopService:
stopService(i);
break;
case R.id.bindService:
bindService(i,connection,BIND_AUTO_CREATE);
break;
case R.id.unbindService:
unbindService(connection);
break;
default:
break;
}
}
};
start.setOnClickListener(l);
stop.setOnClickListener(l);
bind.setOnClickListener(l);
unbind.setOnClickListener(l);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
但是一运行就是报空指针异常,就是注册监听器那里报的。而且只要我把
start.setOnClickListener(l);
stop.setOnClickListener(l);
bind.setOnClickListener(l);
unbind.setOnClickListener(l);这几句注释掉就没问题,控件正常显示。但是一加上这几句注册监听语句,运行就报空指针。Unable to start activity ComponentInfo然后NullPointerException郁闷了,我监听器写的也没问题啊,然后MyService中就是override了service的那几个生命周期函数加上了打印语句而已。帮忙看看啊,这么简单就是不行啊,难道我哪里疏忽了?
public class MyTestActivity extends Activity {
private Button start;
private Button stop;
private Button bind;
private Button unbind;
private ServiceConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.startService);
stop=(Button)findViewById(R.id.stopService);
bind=(Button)findViewById(R.id.bindService);
unbind=(Button)findViewById(R.id.unbindService);
connection=new ServiceConnection(){ @Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
} @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
OnClickListener l=new OnClickListener(){
@Override
public void onClick(View v) {
Intent i=new Intent(MyTestActivity.this,MyService.class);
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startService:
startService(i);
break;
case R.id.stopService:
stopService(i);
break;
case R.id.bindService:
bindService(i,connection,BIND_AUTO_CREATE);
break;
case R.id.unbindService:
unbindService(connection);
break;
default:
break;
}
}
};
start.setOnClickListener(l);
stop.setOnClickListener(l);
bind.setOnClickListener(l);
unbind.setOnClickListener(l);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
但是一运行就是报空指针异常,就是注册监听器那里报的。而且只要我把
start.setOnClickListener(l);
stop.setOnClickListener(l);
bind.setOnClickListener(l);
unbind.setOnClickListener(l);这几句注释掉就没问题,控件正常显示。但是一加上这几句注册监听语句,运行就报空指针。Unable to start activity ComponentInfo然后NullPointerException郁闷了,我监听器写的也没问题啊,然后MyService中就是override了service的那几个生命周期函数加上了打印语句而已。帮忙看看啊,这么简单就是不行啊,难道我哪里疏忽了?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货