Jai Android
Jai Android

Reputation: 2171

Compilation error: static reference to a non-static method

I am unable to call method startVideo() from another class. When I try to compile, I get the following error:

Cannot make a static reference to the non-static method findViewById(int) from the type Activity

Here is the startVideo() method code:

public static void startVideo(){
  startButton = (Button) findViewById(R.id.start_btn);
  startButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      if(width>1000){
        setContentView(R.layout.lesson_large);
      }else{
        setContentView(R.layout.lesson);
      }
      //@@@ FOR INTRO AV @@@//
      VideoView videoView = (VideoView) findViewById(R.id.videoView1);
      MediaController mediaControler = new MediaController(Main.this);
      mediaControler.setAnchorView(videoView);
      Uri introVideo = Uri.parse(statics.urlAv + "AV264.MP4");
      videoView.setMediaController(mediaControler);
      videoView.setVideoURI(introVideo);
      videoView.start();

      //@@@ FOR LESSON/SUBLESSSON AV @@@//
      videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
          VideoView videoView = (VideoView) findViewById(R.id.videoView1);
          MediaController mediaControler = new MediaController(Main.this);
          mediaControler.setAnchorView(videoView);
          Uri video = Uri.parse(statics.urlAv + "AV264.MP4");
          videoView.setMediaController(mediaControler);
          videoView.setVideoURI(video);
          videoView.start();                
        }
      });       
    }       
  });

  menu();
  exit();
}

Any help will really be appreciated. Thanks

Upvotes: 1

Views: 1008

Answers (1)

Headshota
Headshota

Reputation: 21449

you're calling findViewById which is not static method from your method which is static. instance methods can only be called on instance.

Either you should make it non-static, or create an instance of your class there and use it's findViewById

Upvotes: 5

Related Questions