gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:08 25 4
gpt4 key购买 nike

我正在使用 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

最佳答案

遇到同样的问题,在stackoverflow上搜索了很多次。解决方案只是简单地扩展 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_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/youtube_fragment"/>

关于android - com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart 的 NullPointerException(未知来源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com