Null pointer YouTubePlayerSupportFragment

766 views Asked by At

I have problem with "YouTubePlayerSupportFragment"

I get error here :

`YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtubeplayerview);`

Here is the full code: I tried to change a few things, and now I have a little more understandable error. If I find it I will post solcuón.

Update Code 11/06/2015, and I get new Error :

5429-5429/com.onpocket.activamutua E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)

Code :

public class Activity_demo extends YouTubePlayerSupportFragment /*implements YouTubePlayer.OnInitializedListener */{


    private static final int RECOVERY_DIALOG_REQUEST = 10;
    public static final String API_KEY = "AIzaSyBHZjt1vrkjuwCp4YqTsADNQVDf6KvAXr0";
    private String VIDEO_ID = "c_eHTMhEsHU";

    JustifiedTextView tv1;
    ConfigActiva cfg;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_own, container, false);
    TypefaceUtil.overrideFont(getActivity(), "SERIF", ConfigActiva.nameFontStatic);
    ConfigActiva.languageCustom(ConfigActiva.defaultLanguageForce, getActivity());
    init(rootView);    
    return rootView;
}

    public void init(View vRoot){

    //YouTubePlayerView youTubeView = (YouTubePlayerView) getActivity().findViewById(R.id.youtubeplayerview);
    //youTubeView.initialize(API_KEY, this);
    YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtubeplayerview);
    //YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtubeplayerview);
    //youTubePlayerFragment.initialize(API_KEY, this);

    youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
                if (!wasRestored) {
                    youTubePlayer.cueVideo(VIDEO_ID);
                }
            }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
            } else {
                String errorMessage = String.format("YouTube Error (%1$s)", errorReason.toString());
                Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
            }
        }
    });
    }
/*  @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",errorReason.toString());
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);
        }
    }*/

My XML

<fragment 
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:id="@+id/youtubeplayerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

With this line Works, but not load video : updated 11/06/2015 22:21

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
1

There are 1 answers

2
3mpty On

You have to add your fragment dynamically because: Nested fragments Just add some container with id and use this id in Transaction.replace. Also use onCreateView only for inflating and building layout. After that find for views in onViewCreated(View view, ...) from view param, it will make code cleaner.