我的应用程序运行正常,但现在不断崩溃

时间:2019-11-05 12:52:56

标签: java android nullpointerexception youtube-api

我正在使用YouTube数据API v3在我的应用中搜索YouTube视频。 昨晚工作正常,但现在当我单击搜索按钮时,它仍然崩溃。有人可以帮我吗?

我尝试重新生成API密钥,但存在相同的错误

当我更改为:返回mVideoList.size();

  

错误是:E / AndroidRuntime:致命异常:main
  流程:com.example.youtubeexampleproject,PID:7031

     

java.lang.NullPointerException:尝试在

的空对象引用上调用接口方法'int java.util.List.size()'。      

YoutubeAdapter.getItemCount(YoutubeAdapter.java:108)指针指向:return mVideolist.size();

,对于以下错误,代码为:

{
      "code" : 400,   
      "errors" : [ {    
        "domain" : "global",   
        "location" : "fields",   
        "locationType" : "parameter",   
        "message" : "Invalid field selection activity",    
        "reason" : "invalidParameter"   
      } ],   
      "message" : "Invalid field selection activity"
    }

'

query = youtube.search()。list(“ id,snippet”);

        query.setKey(KEY);



        query.setType("video");


        query.setFields("items(id/kind,id/videoId,snippet/title,snippet/description,snippet/thumbnails/high/url/activity)");

    } catch (IOException e) {


        Log.d("YC", "Could not initialize: " + e);
    }
}

public List<VideoItem> search(String keywords) {


    query.setQ(keywords);


    query.setMaxResults(MAXRESULTS);

    try {


        SearchListResponse response = query.execute();


        List<SearchResult> results = response.getItems();


        List<VideoItem> items = new ArrayList<VideoItem>();


        if (results != null) {


            items = setItemsList(results.iterator());
        }

        return items;

公共类YoutubeAdapter扩展了RecyclerView.Adapter {

private Context mContext;
private List<VideoItem> mVideoList;


public class MyViewHolder extends RecyclerView.ViewHolder{

    public ImageView thumbnail;
    public TextView video_title, video_id, video_description;
    public RelativeLayout video_view;

    public MyViewHolder(View view) {

        super(view);


        thumbnail =  view.findViewById(R.id.video_thumbnail);
        video_title =  view.findViewById(R.id.video_title);
        video_id = view.findViewById(R.id.video_id);
        video_description =  view.findViewById(R.id.video_description);
        video_view =  view.findViewById(R.id.video_view);
    }
}


public YoutubeAdapter(Context mContext, List<VideoItem> mVideoList) {
    this.mContext = mContext;
    this.mVideoList = mVideoList;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.video_item, parent, false);

    return new MyViewHolder(itemView);
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {


    final VideoItem singleVideo = mVideoList.get(position);


    holder.video_id.setText("Video ID : "+singleVideo.getId()+" ");
    holder.video_title.setText(singleVideo.getTitle());
    holder.video_description.setText(singleVideo.getDescription());


    Picasso.get()
            .load(singleVideo.getThumbnailURL()).resize(480,270)
            .centerCrop()
            .into(holder.thumbnail);


    holder.video_view.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {


            Intent intent = new Intent();
            intent.setClass(mContext, PlayerActivity.class);


            intent.putExtra("VIDEO_ID", singleVideo.getId());
            intent.putExtra("VIDEO_TITLE",singleVideo.getTitle());
            intent.putExtra("VIDEO_DESC",singleVideo.getDescription());

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            mContext.startActivity(intent);


        }
    });
}

@Override

  public int getItemCount() {    



       return mVideoList == null ? 0 : mVideoList.size();   


   }  
}'

1 个答案:

答案 0 :(得分:0)

从字段中删除snippet/thumbnails/high/url/activity,然后尝试

query.setFields("items(id/kind,id/videoId,snippet/title,snippet/description)");