我正在创建应用程序,您单击该按钮,视频将在其中一个片段类中播放。 在我的Android清单中,我已经要求从互联网获得许可。
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.VideoView;
public class Chapter9 extends Fragment {
WebView webview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.chapternine,container,false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Redox Processes");
}
public void videonine(View view){
if(view==getView().findViewById(R.id.redoxone)){
VideoView videoView =
(VideoView)getView().findViewById(R.id.webnine);
videoView.setVideoPath("https://www.youtube.com/watch?
v=a4NT5iBFuZs");
videoView.start();
}
}
}
我的布局章节是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="270sp"
android:id="@+id/webnine">
</VideoView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/redoxone"
android:text="Play"
android:onClick="videonine"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</LinearLayout>
每次我点击按钮,我的应用程序停止,我的应用程序崩溃 任何帮助将不胜感激。
答案 0 :(得分:0)
您可能不希望在点击事件中不断调用getView
。并且你也应该只在片段附加到它时获得活动
private VideoView videoView;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
videoView = (VideoView) view.findViewById(R.id.webnine);
}
@Override
public void onAttach(Context context) {
getActivity().setTitle("Redox Processes");
}
public void videonine(View view){
if(view.getId() == R.id.redoxone)){
videoView
.setVideoPath("https://www.youtube.com/watch?v=a4NT5iBFuZs");
videoView.start();
}
}