编译错误:对非静态方法的静态引用

时间:2011-08-31 05:58:35

标签: java android compiler-errors

我无法从另一个类调用方法startVideo()。当我尝试编译时,我收到以下错误:

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

这是startVideo()方法代码:

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();
}

真的很感激任何帮助。感谢

1 个答案:

答案 0 :(得分:5)

你正在调用findViewById,这不是你方法中的静态方法。实例方法只能在实例上调用。

要么你应该把它变成非静态的,要么在那里创建你的类的实例并使用它的findViewById