膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错

Posted

技术标签:

【中文标题】膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错【英文标题】:Error inflating class com.google.android.youtube.player.YouTubePlayerView 【发布时间】:2015-02-24 15:06:24 【问题描述】:

我无法开始使用 android Youtube API。 我有一个ActionBarActivity 有一个v4.ViewPager,我想在里面放多个YouTubePlayerSupportFragment。 我得到了Error inflating class com.google.android.youtube.player.YouTubePlayerView,我的提示是继承链中的某些东西是错误的,但我不知道是什么。

到目前为止我的代码: XML:

<FrameLayout android:layout_
android:layout_ >

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_player"
    android:layout_
    android:layout_
    android:background="#fff"
    android:padding="5dp" /> </FrameLayout>

Java:

public class MyYoutubeFragment extends YouTubePlayerSupportFragment 

private static final String API_KEY = "";

private String videoId = "qNggvk8QtaY";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_youtube, container, false);

    YouTubePlayerView youTubePlayerView = (YouTubePlayerView) view.findViewById(R.id.youtube_player);
    youTubePlayerView.initialize(API_KEY, this);

    return view;


@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) 
    Toast.makeText(getActivity(), "Failured to Initialize!", Toast.LENGTH_LONG).show();


@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) 
    player.setPlayerStateChangeListener(playerStateChangeListener);
    player.setPlaybackEventListener(playbackEventListener);

    if (!wasRestored) 
        player.cueVideo(videoId);
    

LogCat:

android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.youtube.player.YouTubePlayerView
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at app.com.example.martinhatzung.movieapp.fragments.MyYoutubeFragment.onCreateView(MyYoutubeFragment.java:54)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:17430)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17430)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1260)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.widget.ScrollView.onMeasure(ScrollView.java:337)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Nat

【问题讨论】:

请填写完整的logcat跟踪 完成了,希望你能帮到我 我也有同样的问题 如果您找到解决方案,也许您可​​以发布它 【参考方案1】:

像这样改变你的班级。也不要忘记使用真正的 API_KEY,在你的代码中它只是一个空字符串

public class MyYoutubeFragment extends YouTubePlayerSupportFragment 
private static final String API_KEY = "";
private String videoId = "qNggvk8QtaY";

public static MyYoutubeFragment newInstance()

    MyYoutubeFragment playerYouTubeFrag = new MyYoutubeFragment();

    playerYouTubeFrag.initialize(API_KEY, this);
    return playerYouTubeFrag;


@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) 
    Toast.makeText(getActivity(), "Failured to Initialize!", Toast.LENGTH_LONG).show();  


@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) 
    player.setPlayerStateChangeListener(playerStateChangeListener);
    player.setPlaybackEventListener(playbackEventListener);

    if (!wasRestored) 
        player.cueVideo(videoId);
    


也加载如下

fragmentManager.beginTransaction()
            .add(R.id.video_view, MyYoutubeFragment.newInstance())
            .commit();

【讨论】:

我在哪里进行交易? 这取决于你想在哪里加载 youtube 片段。例如对于原始问题,它应该在页面更改事件监听器中。

以上是关于膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错的主要内容,如果未能解决你的问题,请参考以下文章

膨胀类 CollapsingToolbarLayout 时出错

膨胀类片段时出错

使用上下文类膨胀布局

如何为android修复“错误膨胀类GeckoEngineView”

在 RecyclerView 上膨胀类 ImageView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错