com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart 处的 NullPointerException(未知来源)

Posted

技术标签:

【中文标题】com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart 处的 NullPointerException(未知来源)【英文标题】:NullPointerException at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source) 【发布时间】:2014-06-11 16:08:04 【问题描述】:

我正在为 viewpager 使用适配器来显示 youtube 视频。

public class ViewPagerYoutubeAdapter  extends FragmentStatePagerAdapter 

String whichlist, site;
private int totalItemsSize;// = 18;

public ViewPagerYoutubeAdapter(FragmentManager supportFragmentManager,
        String whichlist, String site, Context context) 
    super(supportFragmentManager);
    this.whichlist = whichlist;
    this.site = site;
    if (whichlist.equalsIgnoreCase("geetaMessages")) 
        totalItemsSize = context.getResources().getStringArray(
                R.array.geeta_messages_name).length;
    



@Override
public Fragment getItem(int position) 





return ArrayListYoutubeFragment.newInstance(position, whichlist, site);


@Override
public int getCount() 
    return totalItemsSize;


和ArrayListYoutubeFragment如下:暂时忽略参数。我现在只想在所有页面上播放一个视频。

public class ArrayListYoutubeFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener

private String currentVideoID = "video_id";
private YouTubePlayer activePlayer;

public ArrayListYoutubeFragment() 
    // TODO Auto-generated constructor stub




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    View v = inflater.inflate(R.layout.fragments_demo, container, false);

    YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getFragmentManager()
            .findFragmentById(R.id.youtube_fragment);
    youTubePlayerFragment.initialize(Constants.DEVELOPER_KEY, this);
    return v;


@SuppressLint("NewApi")
public static ArrayListYoutubeFragment newInstance(int position,
        String whichlist, String site) 

    ArrayListYoutubeFragment playerYouTubeFrag = new ArrayListYoutubeFragment();

    Bundle bundle = new Bundle();
    bundle.putString("url", whichlist);

    playerYouTubeFrag.setArguments(bundle);

    return playerYouTubeFrag;



// @Override
public void onYouTubeVideoPaused() 
    activePlayer.pause();



@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult arg1) 
    // TODO Auto-generated method stub




@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
        boolean wasRestored) 
    // TODO Auto-generated method stub
     if (!wasRestored) 
          player.cueVideo("nCgQDjiotG0");
        


但我遇到了错误。

12:36:44.422: E/androidRuntime(8625): FATAL EXCEPTION: main
12:36:44.422: E/AndroidRuntime(8625): Process:       com.example.actionbarsherlock, PID: 8625
12:36:44.422: E/AndroidRuntime(8625): java.lang.NullPointerException
12:36:44.422: E/AndroidRuntime(8625):   at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
12:36:44.422: E/AndroidRuntime(8625):   at com.example.viewpager.ArrayListYoutubeFragment.onStart(ArrayListYoutubeFragment.java:31)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.Fragment.performStart(Fragment.java:1524)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-25 12:36:44.422: E/AndroidRuntime(8625):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12:36:44.422: E/AndroidRuntime(8625):   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12:36:44.422: E/AndroidRuntime(8625):   at android.view.View.measure(View.java:16497)
12:36:44.422: E/AndroidRuntime(8625):   at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
12:36:44.422: E/AndroidRuntime(8625):   at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12:36:44.422: E/AndroidRuntime(8625):   at android.view.View.measure(View.java:16497)
12:36:44.422: E/AndroidRuntime(8625):   at 

【问题讨论】:

问题解决了吗? 【参考方案1】:

遇到同样的问题,多次搜索***。 解决方案只是简单地扩展 Fragment 而不是 YouTubePlayerSupportFragment

public class FragmentTrailers extends Fragment implements YouTubePlayer.OnInitializedListener 
    public static final String DEVELOPER_KEY = BuildConfig.YOUTUBE_API;
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private String url;

    YouTubePlayerSupportFragment youTubePlayerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.detail_fragment_trailers, container, false);

        youTubePlayerView = YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_fragment, youTubePlayerView).commit();

        youTubePlayerView.initialize(DEVELOPER_KEY, this);

        return view;
    

XML 布局文件。

      <FrameLayout
            android:layout_
            android:layout_
            android:id="@+id/youtube_fragment"/>

【讨论】:

以上是关于com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart 处的 NullPointerException(未知来源)的主要内容,如果未能解决你的问题,请参考以下文章