-1

I am making app where you click the button and video plays in one of the fragment class. In my android manifest, i have already ask for permission from the internet.

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();
   }

 }
}

And my layout chapter is:

<?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>

Everytime i click button, my app stops and my app crashes Any help will be appreciated.

Sina Darvishi
  • 71
  • 1
  • 11

1 Answers1

0

You might want to not continuously call getView in the click event. And you also should only get the Activity when the Fragment is attached to it

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();
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245