Using both AppCompat and Youtube API in one activi

2019-08-17 10:10发布

I am working on an application in which both AppCompatActivity and Youtube API should be used, but for some reasons I am unable to use both together in same activity when I use AppCompatActicity for my toolbar the Youtube API stops working and if I extends YoutubeBaseActivity the Toolbar does not work, I am also using RecyclerView and Firebase as a database in this applcation, can anybody help me with it and tell me how I can implement AppCompatActivity and youtubeAPI together so they both work, I have researched many forums but they did not provide any useful information on doing so, please help.

I am using Android Studio

Youtube class file :

    public class HomePage extends YouTubeBaseActivity{

        private RecyclerViewPager mrecyclerView;
        private DatabaseReference mDatabaseReference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("holla");
        mrecyclerView = (RecyclerViewPager) findViewById(R.id.list);
        final LinearLayoutManager layout = new LinearLayoutManager(HomePage.this, LinearLayoutManager.VERTICAL, false);
        mrecyclerView.setLayoutManager(layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mAuth = FirebaseAuth.getInstance();
        mrecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (getSupportActionBar().isShowing()) {
                        getSupportActionBar().hide();
                        fab.hide();
                    } else {
                        getSupportActionBar().show();
                        fab.show();
                    }
                    return true;
                } else return false;
            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(

                post.class,
                R.layout.post_row_recycle_home,
                postViewHolder.class,
                mDatabaseReference
        ) {
            @Override
            protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
                viewHolder.setYoutube(model.getYoutube());

            }
        };
        mrecyclerView.setAdapter(firebaseRecyclerAdapter);


    }
    public static class postViewHolder extends RecyclerViewPager.ViewHolder{

        View mView;

        public postViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public void setYoutube(final String youtube){
            final YouTubePlayerView youPlay = (YouTubePlayerView) mView.findViewById(R.id.youtuber);
            youPlay.initialize("SOME KEY",
                    new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                            YouTubePlayer youTubePlayer, boolean b) {

                            youTubePlayer.cueVideo(youtube);
                        }
                        @Override
                        public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                            YouTubeInitializationResult youTubeInitializationResult) {

                        }
                    });
        }

Youtube CardView XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/CardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtuber"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

Log error:

 E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{andromeda.petrochemical/andromeda.petrochemical.HomePage}: java.lang.IllegalArgumentException: No view found for id 0x7f10015f (andromeda.petrochemical:id/watch_video_player) for fragment YouTubePlayerSupportFragment{79044c4 #0 id=0x7f10015f}
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                           Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f10015f (andromeda.petrochemical:id/watch_video_player) for fragment YouTubePlayerSupportFragment{79044c4 #0 id=0x7f10015f}
                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
                                                                              at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                              at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                              at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                              at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                              at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                              at andromeda.petrochemical.HomePage.onStart(HomePage.java:123)
                                                                              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                                              at android.app.Activity.performStart(Activity.java:6679)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

What I want to achieve

enter image description here

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-17 10:45

OK you have your normal HomePage activity that extend AppCompatActivity

At your xml file you have like a container for the videoplayer

   <FrameLayout
        android:id="@+id/watch_video_player"
        android:layout_width="match_parent"
        android:layout_height="@dimen/watf_main_video" />

At your activity add the player

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.watch_video_player, youTubePlayerFragment).commit();

it will get the youtube player then you initialize it as normal

 youTubePlayerFragment.initialize(Constants.API_KEY, new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

                    //load and play the video with current id
                    youTubePlayer.loadVideo(YouTubeApiUtilities.getVideoId(item));
                    youTubePlayer.play();
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                    Toast.makeText(getContext(), R.string.can_not_initialize_youtube_player, Toast.LENGTH_LONG).show();

                }
            });
查看更多
登录 后发表回答