我正试图做到这一点,以便当我单击图像框时,播放的视频应停止线程并开始播放新视频。目前,它给了我一个“ IllegalStateException”异常。
我如何才能停止话题并开始制作新的视频?
public class VideoActivity extends AppCompatActivity {
VideoView videoView;
MediaController mController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.introductie);
}
protected void onClick(View v)
{
videoView.stopPlayback();
switch(v.getId())
{
case R.id.imageView2:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.introductie);
break;
case R.id.imageView3:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.wel_iets_aan_de_hand3);
break;
case R.id.imageView4:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.hoe_moet_je_plassen4);
break;
case R.id.imageView5:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.wanneer_moet_je_plassen5);
break;
case R.id.imageView6:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.hoe_haak_moet_je_plassen6);
break;
case R.id.imageView7:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.piponormaalvrachtwagens7);
break;
case R.id.imageView8:
playVideo("android.resource://"+ getPackageName() + "/" + R.raw.pipoobstipatievrachtwagens8);
break;
}
}
protected void playVideo(String videoName)
{
videoView = findViewById(R.id.videoViewId);
mController = new MediaController(this);
videoView.setVideoPath(videoName);
mController.setAnchorView(videoView);
videoView.setMediaController(mController);
videoView.start();
}
}
9-06 09:55:21.163 7107-7107/com.example.nepalibinod.catherina E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nepalibinod.catherina, PID: 7107
java.lang.IllegalStateException: Could not find method videoOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'imageView2'
答案 0 :(得分:0)
尝试在代码(Java / Kotlin)而不是XML中定义OnClick侦听器。我假设发生的是您在XML内的按钮上分配了android:onClick
,但是android在代码内找不到您的方法。
尝试findViewById
设置视图,并为其分配一个 setOnClickListener
。