![](/img/trans.png)
[英]java.lang.IllegalStateException: Could not find method pay(View) in a parent or ancestor Context for android:onClick attribute defined on view class
[英]java.lang.IllegalStateException: Could not find method play(View) in a parent or ancestor Context for android:onClick attribute defined
我創建了一個程序,在其中添加了一個視頻和 3 個按鈕。 當我在手機上運行時,程序崩潰了。 java.lang.IllegalStateException: Could not find method play(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'video_play' at androidx.appcompat.app.AppCompatViewInflater$ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436) 這是我的代碼
VideoView videoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
videoPlayer = (VideoView)findViewById(R.id.videoPlayer);
String uripath="android.resource://" + getPackageName() + "/" + R.raw.peds;
Uri myVideoUri= Uri.parse(uripath);
videoPlayer.setVideoURI(myVideoUri);
}
public void play(View view){
videoPlayer.start();
}
public void pause(View view){
videoPlayer.pause();
}
public void stop(View view){
videoPlayer.stopPlayback();
videoPlayer.resume();
}
這里是 XML
<Button
android:id="@+id/video_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/videoPlayer"
android:layout_marginTop="0dp"
android:onClick="start"
android:paddingLeft="20dp"
android:text="Play"
android:fontFamily="@font/kras"
android:textColor="#C62828"
android:shadowColor="#000000"
android:shadowDx="4.4"
android:shadowDy="2.4"
android:shadowRadius="1"
android:textSize="19sp"/>
<Button
android:id="@+id/video_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/videoPlayer"
android:layout_marginTop="0dp"
android:layout_marginRight="-214dp"
android:layout_toLeftOf="@id/video_play"
android:onClick="pause"
android:text="Pause"/>
<Button
android:id="@+id/video_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/videoPlayer"
android:layout_marginTop="0dp"
android:layout_marginRight="-324dp"
android:layout_toLeftOf="@id/video_play"
android:onClick="stop"
android:text="Stop"
android:textSize="19sp"/>
在 xml 中添加我們的播放按鈕:
android:onClick="play"
添加到 xml 中的暫停按鈕
android:onClick="pause"
添加到 xml 中的停止按鈕
android:onClick="stop"
更新
看看我上面所說的,並通過 ID 找到按鈕:
private Button playButton;
private Button pauseButton;
private Button stopButton;
videoPlayer = (VideoView)findViewById(R.id.videoPlayer);
//add these here
playButton = (Button)findViewById(R.id.video_play);
pauseButton = (Button)findViewById(R.id.video_pause);
stopButton = (Button)findViewById(R.id.video_stop);
...
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.