2013-07-19 147 views
-1
private OnClickListener startListener = new OnClickListener() { 
    public void onClick(View v) { 
     startService(new Intent(SimpleServiceController.this, 
       SimpleUpdateService.class)); 
     startActivity(new Intent (this, AlarmManagerListening.class)); 
    } 
}; 


startActivity执行时发生问题。它不允许我开始一项活动。我可以知道代码有什么问题吗?onClick开始另一个活动

+1

post logcat plz – KOTIOS

+4

对'startActivity()'方法使用'SimpleServiceController.this'而不是'this'。 – user370305

+1

@ user370305像魅力一样工作!非常感谢! –

回答

2

你面对的这个错误是因为在startActivity(new Intent (this, AlarmManagerListening.class));使用这个的。由于您正在实例化OnClickListener的新实例,因此此引用了侦听器的实例,而不是您作为Context所需的服务的实例。通过利用SimpleServiceController.this,您指定这个引用您的控制器,它是Context的子类,如new Intent()方法所要求的。

0

尝试用这种

public void onClick (View v) { 
    Intent i=new Intent (v.getContext(), SimpleService.class); 
    startActivity(i); 
    } 
相关问题