YouTubePlayerSupportFragment 不能在播放器上添加视图
YouTubePlayerSupportFragment No views can be added on top of the player
我与 YouTubePlayerSupportFragment
斗争了 3 天,试图在滑动片段中使其成为 运行。现在显示这个错误,我尝试了其他方法并得到NullPointerException
。有没有办法将 YouTubePlayerSupportFragment
放入滑动片段中?
java.lang.UnsupportedOperationException: No views can be added on top
of the player
at com.google.android.youtube.player.YouTubePlayerView.a(Unknown
Source)
at com.google.android.youtube.player.YouTubePlayerView.addView(Unknown
Source)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at
android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at
android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at
android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:199)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at
android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at
android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java)
at
android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at
android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at
com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
at android.view.View.measure(View.java)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
at android.view.Choreographer.doCallbacks(Choreographer.java)
at android.view.Choreographer.doFrame(Choreographer.java)
at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invoke(Native Method)
MainActivity.java:
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
PagerAdapter.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
TabFragment1.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
public class TabFragment1 extends Fragment {
private String currentVideoID = "nCgQDjiotG0";
public static final String API_KEY = "XXX";
private YouTubePlayer YPlayer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.youTubePlayer, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
return rootView;
}
tab_fragment_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youTubePlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
尝试
transaction.replace(R.id.youTubePlayer, youTubePlayerFragment).commit();
在您的 onCreateView
中,或者使用
直接检索片段(您的布局中有片段)
YouTubePlayerSupportFragment youTubePlayerFragment = getFragmentManager().findFragmentById(R.id.youTubePlayer);
先代替
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youTubePlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
使用框架布局
<FrameLayout
android:id="@+id/frame_fragment"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
然后在初始化后使用
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_fragment, youTubePlayerFragment);
transaction.commit();
我与 YouTubePlayerSupportFragment
斗争了 3 天,试图在滑动片段中使其成为 运行。现在显示这个错误,我尝试了其他方法并得到NullPointerException
。有没有办法将 YouTubePlayerSupportFragment
放入滑动片段中?
java.lang.UnsupportedOperationException: No views can be added on top of the player at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source) at com.google.android.youtube.player.YouTubePlayerView.addView(Unknown Source) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164) at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) at android.support.v4.view.ViewPager.populate(ViewPager.java:1025) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java) at android.widget.LinearLayout.measureVertical(LinearLayout.java) at android.widget.LinearLayout.onMeasure(LinearLayout.java) at android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:199) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668) at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735) at android.view.View.measure(View.java) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.FrameLayout.onMeasure(FrameLayout.java) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java) at android.widget.LinearLayout.measureVertical(LinearLayout.java) at android.widget.LinearLayout.onMeasure(LinearLayout.java) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.FrameLayout.onMeasure(FrameLayout.java) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java) at android.widget.LinearLayout.measureVertical(LinearLayout.java) at android.widget.LinearLayout.onMeasure(LinearLayout.java) at android.view.View.measure(View.java) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java) at android.widget.FrameLayout.onMeasure(FrameLayout.java) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java) at android.view.View.measure(View.java) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java) at android.view.Choreographer$CallbackRecord.run(Choreographer.java) at android.view.Choreographer.doCallbacks(Choreographer.java) at android.view.Choreographer.doFrame(Choreographer.java) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java) at android.os.Handler.handleCallback(Handler.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invoke(Native Method)
MainActivity.java:
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
PagerAdapter.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
TabFragment1.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
public class TabFragment1 extends Fragment {
private String currentVideoID = "nCgQDjiotG0";
public static final String API_KEY = "XXX";
private YouTubePlayer YPlayer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.youTubePlayer, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
return rootView;
}
tab_fragment_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youTubePlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
尝试
transaction.replace(R.id.youTubePlayer, youTubePlayerFragment).commit();
在您的 onCreateView
中,或者使用
YouTubePlayerSupportFragment youTubePlayerFragment = getFragmentManager().findFragmentById(R.id.youTubePlayer);
先代替
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youTubePlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
使用框架布局
<FrameLayout
android:id="@+id/frame_fragment"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
然后在初始化后使用
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_fragment, youTubePlayerFragment);
transaction.commit();