android – super.onStart()上的NullPointerException

目前我正在编写Fragment,它扩展了扩展YouTubePlayerSupportFragment的自定义SherlockFragment.不幸的是,应用程序崩溃了.

码:

    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)

这段代码有什么问题?

解决方法:

在SDK中并不清楚,但只要我能够解决它,你就不会扩展YouTubePlayerFragment.您只需在您的布局中包含片段(它也可以作为嵌套片段).

基本上你有两种选择:

>扩展YouTubeBaseActivity并扩展包含YouTubePlayerView的布局 – 它可以工作但你不能使用碎片
>使用您最喜欢的活动并为包含YouTubePlayerFragment的布局进行充气.不要延长它.

我已成功将YoutubePlayerFragment添加为嵌套片段(更新:这会导致一些内存泄漏,因此您可能更愿意避免它).您可以在YouTubePlayerFragment周围添加其他组件,但请注意,因为docs表示您无法在播放视频时将此视图与其他视图叠加在一起.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...