我正在使用一个viewpager来加载使用FragmentPagerAdapter提供Youtube片段的youtube视频。我遇到的问题是第一个youtube视频可以加载,但其余的不能。
以下是在我的HomeFragment中创建viewpager的方法:
public void setupUI(View view)
{
// viewPager = (ViewPager) findViewById(R.id.pager);
viewpager = (ViewPager) view.findViewById(R.id.pager);
pager_adapter = new ViewPagerAdapter(getActivity(),rank,country,population,flag);
FragmentActivity fragact = myContext;
FragmentPagerAdapter fragmentAdapter = new FragmentAdapter(fragact.getSupportFragmentManager());
viewpager.setAdapter(fragmentAdapter);
//pager_adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag);
// Binds the Adapter to the ViewPager
//viewPager.setAdapter(adapter);
CirclePageIndicator titleIndicator = (CirclePageIndicator)view.findViewById(R.id.indicator);
titleIndicator.setViewPager(viewpager);
}
这是我的片段适配器。
public class FragmentAdapter extends FragmentPagerAdapter{
public FragmentAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
String test = Integer.toString(position);
ImageFragment imgfrag = ImageFragment.newInstance(test,test);
return imgfrag;
}
@Override
public int getCount() {
return FRAGMENT_COUNT;
}
这里是加载YouTube视频的片段。
public class ImageFragment extends Fragment implements YouTubePlayer.OnInitializedListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private YouTubePlayer activePlayer;
public static final String YOUTUBE_API_KEY = "xxxxxxxxxxxxxxxxxxx";
// TODO: Rename and change types of parameters
private String mParam1;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ImageFragment.
*/
// TODO: Rename and change types and number of parameters
public static ImageFragment newInstance(String param1, String param2) {
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
public ImageFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View RootView = inflater.inflate(R.layout.fragment_image, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = new YouTubePlayerSupportFragment();
youTubePlayerFragment.initialize(YOUTUBE_API_KEY,this);
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.youtubeplayerfragment,youTubePlayerFragment,YouTubePlayerFragment.class.getSimpleName());
fragmentTransaction.commit();
RootView.findViewById(R.id.youtubeplayerfragment).setVisibility(View.VISIBLE);
return RootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("nCgQDjiotG0");
Log.v("Youtube", "Success occured one");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
String errorMessage = String.format("error reason", youTubeInitializationResult.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(YOUTUBE_API_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerSupportFragment) getFragmentManager().findFragmentByTag(YouTubePlayerFragment.class.getSimpleName());
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
以下是我的日志记录:
08-26 13:22:11.020 348-348/com.festivalguide.v2.edmx E/ActivityThread﹕ Activity com.festivalguide.v2.edmx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@1dd9bd3c that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.festivalguide.v2.edmx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@1dd9bd3c that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1077)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:971)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1774)
at android.app.ContextImpl.bindService(ContextImpl.java:1757)
at android.content.ContextWrapper.bindService(ContextWrapper.java:539)
at com.google.android.youtube.player.internal.r.e(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerSupportFragment.a(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerSupportFragment.onCreateView(Unknown Source)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
at android.view.View.measure(View.java:17547)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
非常感谢您的帮助。
提前致谢。
(需要翻译的内容)