4

目前我正在编写扩展自定义 SherlockFragment 的片段,该扩展扩展了 YouTubePlayerSupportFragment。不幸的是,应用程序崩溃了。

代码:

    public static class YouTubeVideo extends SherlockYouTubeFragment
         implements YouTubePlayer.OnInitializedListener{
private TextView titleview;
private TextView descview;
private TextView countview;
private TextView timeview;
private Bundle data;

 private YouTubePlayer ytp;
private String title;
private String id;
private String desc;
private String count;
private String time;
private int timeint;
private int timem;
private int times;
private Tracker myTracker;
private ShareActionProvider mShareActionProvider;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    data = getArguments();
    return inflater.inflate(R.layout.youtubeview, container, false);
}

@Override
public void onStart() {
     titleview = (TextView) getView().findViewById(R.id.title);
     descview = (TextView) getView().findViewById(R.id.descriptiontext);
     countview = (TextView) getView().findViewById(R.id.viewcounttext);
     timeview = (TextView) getView().findViewById(R.id.timetext);

     title = data.getString("title");
     id = data.getString("id");
     desc = data.getString("description");
     count = data.getString("viewcount");
     time = data.getString("time");
     EasyTracker.getInstance().setContext(this.getActivity());
     myTracker = EasyTracker.getInstance().getTracker();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager
             .beginTransaction();

     YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
      fragment.initialize(ID, this);
     fragmentTransaction.add(R.id.youtubeplayer, fragment);
     fragmentTransaction.commit();
  titleview.setText(title);
  descview.setText(desc);
  countview.setText(count);
  try {
      timeint = Integer.parseInt(time);
      timem = timeint/60;
      times = timeint - (timem*60);      
  } catch(NumberFormatException nfe) {
    // Handle parse error.
  }
  if (times<10){
timeview.setText(String.valueOf(timem)+":0"+String.valueOf(times));
 }
  else{
      timeview.setText(String.valueOf(timem)+":"+String.valueOf(times));
  }
  super.onStart();
 }

 @Override
 public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
  Toast.makeText(this.getActivity(), "Uruchamianie filmu nie powiodło się", Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasrestored) {
  ytp = player;
  player.addFullscreenControlFlag(4);
  if(ytp !=null){
        ytp.loadVideo(id);
       }
 }
 public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
     inflater.inflate(R.menu.video_menu, menu);
     mShareActionProvider = (ShareActionProvider)menu.findItem(R.id.share).getActionProvider();
     mShareActionProvider.setShareIntent(createShareIntent());

     return true;
 }
 private Intent createShareIntent() {
     myTracker.trackEvent("videoview", "share", title + id, (long) 3);

        Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, title + " - http://ignastv.tk/id/" + id);
    sendIntent.setType("text/plain");
    mShareActionProvider.setShareIntent(sendIntent);
    return sendIntent;
}

 public boolean onOptionsItemSelected(MenuItem item) {
     int itemId = item.getItemId();

        if(itemId == R.id.vidsite){
            myTracker.trackEvent("videoview", "open-in", title + id, (long) 3);
            Intent govideo = new Intent(Intent.ACTION_VIEW);
            govideo.setData(Uri.parse(id));
            startActivity(govideo);
            return true;}

        else {
            return super.onOptionsItemSelected(item);
        }

}

}

最有趣的是 LogCat 显示有 NullPointerException 发生在一行中:

    super.onStart();

这是LogCat:

    04-27 20:19:30.055: E/AndroidRuntime(14706): FATAL EXCEPTION: main
    04-27 20:19:30.055: E/AndroidRuntime(14706): java.lang.NullPointerException
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at com.app.app.MainActivity$YouTubeVideo.onStart(MainActivity.java:519)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.Fragment.performStart(Fragment.java:1332)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.os.Handler.handleCallback(Handler.java:725)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.os.Handler.dispatchMessage(Handler.java:92)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.os.Looper.loop(Looper.java:137)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at android.app.ActivityThread.main(ActivityThread.java:5039)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at java.lang.reflect.Method.invoke(Method.java:511)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    04-27 20:19:30.055: E/AndroidRuntime(14706):    at dalvik.system.NativeStart.main(Native Method)

这段代码有什么问题?

4

2 回答 2

3

SDK 中并不清楚,但据我所知,您不会扩展 YouTubePlayerFragment。您只需在布局中包含片段(它也可以作为嵌套片段)。

基本上你有两种选择:

  • 扩展 YouTubeBaseActivity 并扩展包含 YouTubePlayerView 的布局 - 它可以工作,但您不能使用 Fragments

  • 使用您最喜欢的 Activity 并扩展包含 YouTubePlayerFragment 的布局。不要延长它

我也成功地将 YoutubePlayerFragment 添加为嵌套片段(更新:这会导致一些内存泄漏,因此您可能更愿意避免它)。您可以在 YouTubePlayerFragment 周围添加其他组件,但要小心,因为文档说您不能在播放视频时将此视图与其他视图重叠。

因此,要解决您的问题,请不要扩展 YouTubePlayerFragment,只需将其包含在您的布局中即可。通过在此片段上调用初始化,您将在回调中获得一个 YouTubePlayer,您可以使用它来控制视频。

于 2014-07-29T12:22:30.200 回答
0

遇到同样的问题,多次搜索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"/>
于 2016-07-22T09:29:19.710 回答