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

发布于 2024-12-02 03:22:58 字数 1628 浏览 0 评论 0原文

我无法从另一个类调用方法 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();
}

任何帮助将不胜感激。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-12-09 03:22:58

您正在从静态方法调用 findViewById 这不是静态方法。实例方法只能在实例上调用。

您应该将其设置为非静态,或者在那里创建类的实例并使用它的 findViewById

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文