编译错误:静态引用非静态方法
我无法从另一个类调用方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在从静态方法调用 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