view = inflater.inflate(R.layout.video_list_item,parent,false);导致力量关闭错误

时间:2013-11-21 20:28:04

标签: java android youtube forceclose android-youtube-api

我在下一行收到强制关闭错误:

view = inflater.inflate(R.layout.video_list_item, parent, false);

当java类被设置为活动时,似乎我的应用程序正在查找视图(正如我在Google中使用的示例中所示)。我正在使用YouTube API中的VideoListDemoActivity.java Android代码在此处找到:

https://code.google.com/p/youtube-api-samples/source/browse/android-player/src/com/examples/youtubeapidemo/VideoListDemoActivity.java

我只想知道两件事:

一个。为什么这不是设置为YouTube API中的视图 湾我可以做些什么来让我的应用程序中的代码工作,以避免此强制关闭错误

当地消息来源:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      VideoEntry entry = entries.get(position);

      // There are three cases here
      if (view == null) {
        // 1) The view has not yet been created - we need to initialize the YouTubeThumbnailView.
        view = inflater.inflate(R.layout.video_list_item, parent, false);
        YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
        thumbnail.setTag(entry.videoId);
        thumbnail.initialize(DeveloperKey.DEVELOPER_KEY, thumbnailListener);
      } else {
        YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
        YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(thumbnail);
        if (loader == null) {
          // 2) The view is already created, and is currently being initialized. We store the
          //    current videoId in the tag.
          thumbnail.setTag(entry.videoId);
        } else {
          // 3) The view is already created and already initialized. Simply set the right videoId
          //    on the loader.
          thumbnail.setImageResource(R.drawable.loading_thumbnail);
          loader.setVideo(entry.videoId);
        }
      }
      TextView label = ((TextView) view.findViewById(R.id.text));
      label.setText(entry.text);
      label.setVisibility(labelsVisible ? View.VISIBLE : View.GONE);
      return view;
    }

logcat的:

11-21 14:39:52.837: D/ActivityThread(26592): setTargetHeapUtilization:0.25
11-21 14:39:52.837: D/ActivityThread(26592): setTargetHeapIdealFree:8388608
11-21 14:39:52.837: D/ActivityThread(26592): setTargetHeapConcurrentStart:2097152
11-21 14:39:53.067: D/AbsListView(26592): Get MotionRecognitionManager
11-21 14:39:53.367: I/Adreno200-EGLSUB(26592): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-21 14:39:53.387: E/(26592): <s3dReadConfigFile:75>: Can't open file for reading
11-21 14:39:53.387: E/(26592): <s3dReadConfigFile:75>: Can't open file for reading
11-21 14:39:53.387: W/dalvikvm(26592): threadid=1: thread exiting with uncaught exception (group=0x41800438)
11-21 14:39:53.407: E/AndroidRuntime(26592): FATAL EXCEPTION: main
11-21 14:39:53.407: E/AndroidRuntime(26592): java.lang.NullPointerException
11-21 14:39:53.407: E/AndroidRuntime(26592):    at com.example.idg2.VideoListDemoActivity$PageAdapter.getView(VideoListDemoActivity.java:295)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.AbsListView.obtainView(AbsListView.java:2453)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.ListView.fillDown(ListView.java:678)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.ListView.fillFromTop(ListView.java:739)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.ListView.layoutChildren(ListView.java:1628)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.AbsListView.onLayout(AbsListView.java:2288)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.View.layout(View.java:13957)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2003)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1824)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4553)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.Choreographer.doFrame(Choreographer.java:525)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.os.Handler.handleCallback(Handler.java:615)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.os.Looper.loop(Looper.java:137)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at android.app.ActivityThread.main(ActivityThread.java:4950)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at java.lang.reflect.Method.invoke(Method.java:511)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-21 14:39:53.407: E/AndroidRuntime(26592):    at dalvik.system.NativeStart.main(Native Method)
11-21 14:40:29.967: D/ActivityThread(26730): setTargetHeapUtilization:0.25
11-21 14:40:29.967: D/ActivityThread(26730): setTargetHeapIdealFree:8388608
11-21 14:40:29.967: D/ActivityThread(26730): setTargetHeapConcurrentStart:2097152
11-21 14:40:30.187: D/AbsListView(26730): Get MotionRecognitionManager
11-21 14:40:30.467: I/Adreno200-EGLSUB(26730): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-21 14:40:30.487: E/(26730): <s3dReadConfigFile:75>: Can't open file for reading
11-21 14:40:30.497: E/(26730): <s3dReadConfigFile:75>: Can't open file for reading
11-21 14:40:30.507: W/dalvikvm(26730): threadid=1: thread exiting with uncaught exception (group=0x41800438)
11-21 14:40:30.507: E/AndroidRuntime(26730): FATAL EXCEPTION: main
11-21 14:40:30.507: E/AndroidRuntime(26730): java.lang.NullPointerException
11-21 14:40:30.507: E/AndroidRuntime(26730):    at com.example.idg2.VideoListDemoActivity$PageAdapter.getView(VideoListDemoActivity.java:299)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.AbsListView.obtainView(AbsListView.java:2453)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.ListView.fillDown(ListView.java:678)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.ListView.fillFromTop(ListView.java:739)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.ListView.layoutChildren(ListView.java:1628)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.AbsListView.onLayout(AbsListView.java:2288)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.View.layout(View.java:13957)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2003)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1824)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4553)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.Choreographer.doFrame(Choreographer.java:525)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.os.Handler.handleCallback(Handler.java:615)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.os.Looper.loop(Looper.java:137)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at android.app.ActivityThread.main(ActivityThread.java:4950)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at java.lang.reflect.Method.invoke(Method.java:511)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-21 14:40:30.507: E/AndroidRuntime(26730):    at dalvik.system.NativeStart.main(Native Method)

原始来源:

https://code.google.com/p/youtube-api-samples/source/browse/android-player/src/com/examples/youtubeapidemo/VideoListDemoActivity.java

附加说明:

它在线上崩溃了:

YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);

完整来源:

/**
 * An Activity showing how to manage multiple YouTubeThumbnailViews in an adapter for display
 * in a List. When the list items are clicked, the video is played by using a YouTubePlayerFragment.
 * <p>
 * The demo supports custom fullscreen and transitioning between portrait and landscape without
 * rebuffering.
 */


public final class VideoListDemoActivity extends Activity implements OnFullscreenListener {

  /** The duration of the animation sliding up the video in portrait. */
  private static final int ANIMATION_DURATION_MILLIS = 300;
  /** The padding between the video list and the video in landscape orientation. */
  private static final int LANDSCAPE_VIDEO_PADDING_DP = 5;

  private VideoListFragment listFragment;
  private VideoFragment videoFragment;

  private View videoBox;
  private View closeButton;

  private boolean isFullscreen;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.video_list_demo);

    listFragment = (VideoListFragment) getFragmentManager().findFragmentById(R.id.list_fragment);
    videoFragment =
        (VideoFragment) getFragmentManager().findFragmentById(R.id.video_fragment_container);

    videoBox = findViewById(R.id.video_box);
    closeButton = findViewById(R.id.close_button);

    videoBox.setVisibility(View.INVISIBLE);

    layout();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    layout();
  }

  @Override
  public void onFullscreen(boolean isFullscreen) {
    this.isFullscreen = isFullscreen;

    layout();
  }

  /**
   * Sets up the layout programatically for the three different states. Portrait, landscape or
   * fullscreen+landscape. This has to be done programmatically because we handle the orientation
   * changes ourselves in order to get fluent fullscreen transitions, so the xml layout resources
   * do not get reloaded.
   */
  private void layout() {
    boolean isPortrait =
        getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;

    listFragment.getView().setVisibility(isFullscreen ? View.GONE : View.VISIBLE);
    listFragment.setLabelVisibility(isPortrait);
    closeButton.setVisibility(isPortrait ? View.VISIBLE : View.GONE);

    if (isFullscreen) {
      videoBox.setTranslationY(0); // Reset any translation that was applied in portrait.
      setLayoutSize(videoFragment.getView(), MATCH_PARENT, MATCH_PARENT);
      setLayoutSizeAndGravity(videoBox, MATCH_PARENT, MATCH_PARENT, Gravity.TOP | Gravity.LEFT);
    } else if (isPortrait) {
      setLayoutSize(listFragment.getView(), MATCH_PARENT, MATCH_PARENT);
      setLayoutSize(videoFragment.getView(), MATCH_PARENT, WRAP_CONTENT);
      setLayoutSizeAndGravity(videoBox, MATCH_PARENT, WRAP_CONTENT, Gravity.BOTTOM);
    } else {
      videoBox.setTranslationY(0); // Reset any translation that was applied in portrait.
      int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
      setLayoutSize(listFragment.getView(), screenWidth / 4, MATCH_PARENT);
      int videoWidth = screenWidth - screenWidth / 4 - dpToPx(LANDSCAPE_VIDEO_PADDING_DP);
      setLayoutSize(videoFragment.getView(), videoWidth, WRAP_CONTENT);
      setLayoutSizeAndGravity(videoBox, videoWidth, WRAP_CONTENT,
          Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    }
  }

  public void onClickClose(@SuppressWarnings("unused") View view) {
    listFragment.getListView().clearChoices();
    listFragment.getListView().requestLayout();
    videoFragment.pause();
    videoBox.animate()
        .translationYBy(videoBox.getHeight())
        .setDuration(ANIMATION_DURATION_MILLIS)
        .withEndAction(new Runnable() {
          @Override
          public void run() {
            videoBox.setVisibility(View.INVISIBLE);
          }
        });
  }

  /**
   * A fragment that shows a static list of videos.
   */
  public static final class VideoListFragment extends ListFragment {

    private static final List<VideoEntry> VIDEO_LIST;
    static {
      List<VideoEntry> list = new ArrayList<VideoEntry>();
      list.add(new VideoEntry("YouTube Collection", "Y_UmWdcTrrc"));
      list.add(new VideoEntry("GMail Tap", "1KhZKNZO8mQ"));
      list.add(new VideoEntry("Chrome Multitask", "UiLSiqyDf4Y"));
      list.add(new VideoEntry("Google Fiber", "re0VRK6ouwI"));
      list.add(new VideoEntry("Autocompleter", "blB_X38YSxQ"));
      list.add(new VideoEntry("GMail Motion", "Bu927_ul_X0"));
      list.add(new VideoEntry("Translate for Animals", "3I24bSteJpw"));
      VIDEO_LIST = Collections.unmodifiableList(list);
    }

    private PageAdapter adapter;
    private View videoBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      adapter = new PageAdapter(getActivity(), VIDEO_LIST);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);

      videoBox = getActivity().findViewById(R.id.video_box);
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
      String videoId = VIDEO_LIST.get(position).videoId;

      VideoFragment videoFragment =
          (VideoFragment) getFragmentManager().findFragmentById(R.id.video_fragment_container);
      videoFragment.setVideoId(videoId);

      // The videoBox is INVISIBLE if no video was previously selected, so we need to show it now.
      if (videoBox.getVisibility() != View.VISIBLE) {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
          // Initially translate off the screen so that it can be animated in from below.
          videoBox.setTranslationY(videoBox.getHeight());
        }
        videoBox.setVisibility(View.VISIBLE);
      }

      // If the fragment is off the screen, we animate it in.
      if (videoBox.getTranslationY() > 0) {
        videoBox.animate().translationY(0).setDuration(ANIMATION_DURATION_MILLIS);
      }
    }

    @Override
    public void onDestroyView() {
      super.onDestroyView();

      adapter.releaseLoaders();
    }

    public void setLabelVisibility(boolean visible) {
      adapter.setLabelVisibility(visible);
    }

  }

  /**
   * Adapter for the video list. Manages a set of YouTubeThumbnailViews, including initializing each
   * of them only once and keeping track of the loader of each one. When the ListFragment gets
   * destroyed it releases all the loaders.
   */
  private static final class PageAdapter extends BaseAdapter {

    private final List<VideoEntry> entries;
    private final List<View> entryViews;
    private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
    private final LayoutInflater inflater;
    private final ThumbnailListener thumbnailListener;

    private boolean labelsVisible;

    public PageAdapter(Context context, List<VideoEntry> entries) {
      this.entries = entries;

      entryViews = new ArrayList<View>();
      thumbnailViewToLoaderMap = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();
      inflater = LayoutInflater.from(context);
      thumbnailListener = new ThumbnailListener();

      labelsVisible = true;
    }

    public void releaseLoaders() {
      for (YouTubeThumbnailLoader loader : thumbnailViewToLoaderMap.values()) {
        loader.release();
      }
    }

    public void setLabelVisibility(boolean visible) {
      labelsVisible = visible;
      for (View view : entryViews) {
        view.findViewById(R.id.text).setVisibility(visible ? View.VISIBLE : View.GONE);
      }
    }

    @Override
    public int getCount() {
      return entries.size();
    }

    @Override
    public VideoEntry getItem(int position) {
      return entries.get(position);
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      VideoEntry entry = entries.get(position);

      // There are three cases here
      if (view == null) {
        // 1) The view has not yet been created - we need to initialize the YouTubeThumbnailView.
        view = inflater.inflate(R.layout.video_list_item, parent, false);
        YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
        thumbnail.setTag(entry.videoId);
        thumbnail.initialize(DeveloperKey.DEVELOPER_KEY, thumbnailListener);
      } else {
        YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
        YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(thumbnail);
        if (loader == null) {
          // 2) The view is already created, and is currently being initialized. We store the
          //    current videoId in the tag.
          thumbnail.setTag(entry.videoId);
        } else {
          // 3) The view is already created and already initialized. Simply set the right videoId
          //    on the loader.
          thumbnail.setImageResource(R.drawable.loading_thumbnail);
          loader.setVideo(entry.videoId);
        }
      }
      TextView label = ((TextView) view.findViewById(R.id.text));
      label.setText(entry.text);
      label.setVisibility(labelsVisible ? View.VISIBLE : View.GONE);
      return view;
    }

    private final class ThumbnailListener implements
        YouTubeThumbnailView.OnInitializedListener,
        YouTubeThumbnailLoader.OnThumbnailLoadedListener {

      @Override
      public void onInitializationSuccess(
          YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
        loader.setOnThumbnailLoadedListener(this);
        thumbnailViewToLoaderMap.put(view, loader);
        view.setImageResource(R.drawable.loading_thumbnail);
        String videoId = (String) view.getTag();
        loader.setVideo(videoId);
      }

      @Override
      public void onInitializationFailure(
          YouTubeThumbnailView view, YouTubeInitializationResult loader) {
        view.setImageResource(R.drawable.no_thumbnail);
      }

      @Override
      public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
      }

      @Override
      public void onThumbnailError(YouTubeThumbnailView view, ErrorReason errorReason) {
        view.setImageResource(R.drawable.no_thumbnail);
      }
    }

  }

  public static final class VideoFragment extends YouTubePlayerFragment
      implements OnInitializedListener {

    private YouTubePlayer player;
    private String videoId;

    public static VideoFragment newInstance() {
      return new VideoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      initialize(DeveloperKey.DEVELOPER_KEY, this);
    }

    @Override
    public void onDestroy() {
      if (player != null) {
        player.release();
      }
      super.onDestroy();
    }

    public void setVideoId(String videoId) {
      if (videoId != null && !videoId.equals(this.videoId)) {
        this.videoId = videoId;
        if (player != null) {
          player.cueVideo(videoId);
        }
      }
    }

    public void pause() {
      if (player != null) {
        player.pause();
      }
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean restored) {
      this.player = player;
      player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
      player.setOnFullscreenListener((VideoListDemoActivity) getActivity());
      if (!restored && videoId != null) {
        player.cueVideo(videoId);
      }
    }

    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
      this.player = null;
    }

  }

  private static final class VideoEntry {
    private final String text;
    private final String videoId;

    public VideoEntry(String text, String videoId) {
      this.text = text;
      this.videoId = videoId;
    }
  }

  // Utility methods for layouting.

  private int dpToPx(int dp) {
    return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
  }

  private static void setLayoutSize(View view, int width, int height) {
    LayoutParams params = view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
  }

  private static void setLayoutSizeAndGravity(View view, int width, int height, int gravity) {
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    params.gravity = gravity;
    view.setLayoutParams(params);
  }

}

1 个答案:

答案 0 :(得分:0)

布局文件夹中是否有名为video_list_item的布局?如果没有那么这就是问题所在。如果你希望使用android提供的样本布局,那么你需要用R.layout.video_list_item替换android.R.layout.video_list_item(我不确定android是否提供这种布局......只是预感)。