2011-10-31 2 views
0

이 간단한 코드 (im : 초급 :)를 실행하려고합니다.활동을 호출하려하지만 onClick에 문제가있는 경우

실행을 시도합니다. // 텍스트는 일반적으로 버튼에 사용됩니다. 그러나, 나는 시도하고 싶었던이 전환 기법을 보았을 때보다 효율적으로 보였다. 그러나, onClick (뭔가)에 관한 오류가 발생하고 'void는 잘못된 형식입니다.'). 나는 이것이 무엇을 일으킬 수 있는지 전혀 모른다. 버튼에 액세스하고 싶습니다. 아무도 말해주지 왜?

감사합니다.

package com.experiment.fewops; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 

    public class FewOptions extends Activity { 
     /** Called when the activity is first created. */ 

     final Button sexy = (Button) findViewById(R.id.buttonSexy); 
     final Button text = (Button) findViewById(R.id.buttonText); 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

    //  sexy.setOnClickListener(new View.OnClickListener() { 
    //   
    //   @Override 
    //   public void onClick(View v) { 
    //    Intent intent = new Intent(this, SexyPage.class); 
    //    startActivity(intent); 
    //   } 
    //  }); 

    @Override 
    public void onClick(View v) { 


     switch(v.getId()){ 
     case R.id.buttonSexy: 
      Intent intent = new Intent(this,SexyPage.class); 
      startActivity(intent); 
      break; 
     } 

    }; 


} 

}

답변

1

이잖아 : 첫째, @Saiesh 말했다, 당신은에 클릭 리스너를 구현하려는 경우 클래스 수준에서 클래스 선언을 변경하여 OnClickListener를 구현해야합니다. 그래서 선언

public class FewOptions extends Activity implements OnClickListener{ 

처럼 두 번째 문제 (당신이 무효가 나쁜 유형 인에 대한 오류를 얻고있는 이유를) 당신이 당신의에서 onCreate 메소드의 본문에 온 클릭 메소드를 선언하고 있다는 것입니다 찾을 것입니다. onClick 메서드의 선언을 onCreate 메서드의 닫는 중괄호 (}) 외부로 이동하면 오류가 사라집니다.

하나 더 참고 :

sexy.setOnClickListener(this); 
: 당신은 버튼의 클릭 리스너로 클래스를 추가하는 것을 잊지 마세요, 위의 2 수정을 한 후에
0

당신은 바로 상황을 전달해야

Intent intent = new Intent(FewOptions.this,SexyPage.class); 
FewOptions.this.startActivity(intent); 
0

잘 솔루션이 클래스는 OnClickListener를 인터페이스를 구현해야이 온 클릭() 메서드를 사용하여 즉. 따라서 클래스 제목이

public class FewOptions extends Activity implements onClickListener 
{ 
     //Eclipse will automatically ask you to override the onClick() method 
} 

과 같이해야합니다 그래서 실제로이 문제는 여기에있다 솔루션 :

관련 문제