마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

버튼 리스너 오류 도와주세요

0 추천
참고 : http://ccdev.tistory.com/14
 
public class ConfigActivity extends Activity {

    private Button onBtn, offBtn;

    @Override
    protected void onCreate(Bundle savedInstanceSate){
        super.onCreate(savedInstanceSate);
        setContentView(R.layout.activity_main);

        onBtn = (Button)findViewById(R.id.btn1);
        offBtn = (Button)findViewById(R.id.btn2);

        onBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(this, ScreenService.class);
                startService(intent);
            }
        });
        offBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(this, ScreenService.class);
                stopService(intent);
            }
        });
    }
}
오류
Error:(27, 33) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<ScreenService>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
Error:(34, 33) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<ScreenService>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
익명사용자 님이 2015년 11월 6일 질문

1개의 답변

0 추천
Intent intent = new Intent(this, ScreenService.class);

위의 코드를 아래와 같이 바꾸세요

Intent intent = new Intent(ConfigActivity.this, ScreenService.class);

 

그냥 this 를 쓰시면 new View.OnClickListener() 로 생성한 인스턴스의 this가 넘어옵니다.
바램 (19,650 포인트) 님이 2015년 11월 6일 답변
아 해결됬네요 ㅠㅠ 감사합니다 !!!!
...