使用回收站视图播放YouTube视频

时间:2017-02-03 06:55:59

标签: android youtube

我正在尝试从数据库获取youtube id以播放视频。然后在我的布局中使用recyclerview我想在我的设备上显示它。这是通过使用字符串数组。我只想在recyclerview中播放youtube的视频。但我一直在收到错误

  

“java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'。

我的代码如下:

package com.echessa.videotube;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by ofaroque on 8/13/15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.VideoInfoHolder> {
    ConnectionClass connectionClass;
    //these ids are the unique id for each video
    String[] VideoID;
    // String[] VideoID = {"zlQMI01A3gg","zlQMI01A3gg"};

  {
     connectionClass = new ConnectionClass();
      ResultSet Video = null;
      String data = "";
      String msg = "";

      Connection con = connectionClass.CONN();
      {
          String command = "SELECT TOP 1 videoData FROM tab1";
          try
          {
              Statement stmt = con.createStatement();
              Video = stmt.executeQuery(command);
              if(Video != null)
              {
                  //while (Video.next())
                      int size = VideoID.length;
                  for (int i=0; i<size; i++)
                  {
                      data = Video.getString(1);
                      VideoID[1] = data;
                     // Toast.makeText(ctx.getApplicationContext(), data , Toast.LENGTH_SHORT).show();
                  }
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
    }





    @Override
    public int getItemCount() {
        return VideoID.length;
       /* try {
            return VideoID.length;
        } catch (Exception e) {
          return 0;
        }*/
    }

我的主要活动类别是

package com.echessa.videotube;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {

    private static String TAG = MainActivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manisha);


        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
        recyclerView.setHasFixedSize(true);
        //to use RecycleView, you need a layout manager. default is LinearLayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        RecyclerAdapter adapter = new RecyclerAdapter(this);
        recyclerView.setAdapter(adapter);

    }
}

通过String[] VideoID = {"zlQMI01A3gg","zlQMI01A3gg"};播放视频时,视频已成功播放,但在循环中添加此视频时出现错误

2 个答案:

答案 0 :(得分:1)

我在recyclerview上使用YouTubePlayerSupportFragment播放它可以正常工作。

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    Log.i("state", "work");
  /*  if (liststate.get(position)!=0) {*/
        final YoutubeVideoViewHolder holder1 = (YoutubeVideoViewHolder) holder;

        YoutubeVideo youtubeVideo = mYoutubeVideos.get(position);
        YouTubeThumbnailView videoThumb = holder1.youTubeThumbnailView;
        YouTubeThumbnailLoader loader = mThumbnailViewToLoaderMap.get(videoThumb);
        if (loader == null) {
            videoThumb.setTag(youtubeVideo.getVideoId());
            videoThumb.initialize(mContext.getString(R.string.YOUTUBE_DEVELOPER_KEY), mThumbnailListener);
        } else {
            videoThumb.setImageResource(R.drawable.loading_thumbnail);
            loader.setVideo(youtubeVideo.getVideoId());
        }
        int newid = (int) System.currentTimeMillis();
        ((YoutubeVideoViewHolder) holder).frameLayout.setId(newid);
        holder1.youtubeVideoPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder1.youTubeThumbnailView.setVisibility(View.GONE);
                holder1.youtubeVideoPlayBtn.setVisibility(View.GONE);

                if (oldid != 0) {
                    fragmentManager.findFragmentById(oldid);
                    fragmentManager.beginTransaction().remove(fragmentManager.findFragmentById(oldid)).commit();
                    v1.setVisibility(View.VISIBLE);
                    v2.setVisibility(View.VISIBLE);

                }

                YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

                fragmentManager.beginTransaction().add(((YoutubeVideoViewHolder) holder).frameLayout.getId(), youTubePlayerFragment).commit();

                initializedListener = new OnInitializedListener(position);
                youTubePlayerFragment.initialize(mContext.getString(R.string.YOUTUBE_DEVELOPER_KEY), initializedListener);
                oldid = ((YoutubeVideoViewHolder) holder).frameLayout.getId();
                v1 = holder1.youTubeThumbnailView;
                v2 = holder1.youtubeVideoPlayBtn;
            }
        });
        if (!holder1.frameLayout.isShown() && liststate.get(position) != 0) {
            Log.i("state", "showbtn");
            holder1.youTubeThumbnailView.setVisibility(View.VISIBLE);
            holder1.youtubeVideoPlayBtn.setVisibility(View.VISIBLE);
        }

    if (liststate.get(position) == 1 && !statefull) {
        Log.i("state", "truescreen");
        youTubePlayer.setFullscreen(true);


    } else if (liststate.get(position) == 0 && statefull) {
        Log.i("state", "falsescreen");
        Log.i("state",Constant.fullscreenposition+"");
        youTubePlayer.setFullscreen(false);

    }

}

答案 1 :(得分:0)

您已声明VideoID String[] VideoID;,但您正在onCreateViewHolder()中使用另一个本地变量

String[] VideoID = {"zlQMI01A3gg", "zlQMI01A3gg"};

不要在那里声明另一个变量,只需使用

VideoID = {"zlQMI01A3gg", "zlQMI01A3gg"};