Android的ListView中显示YouTubePlayer

Android的ListView中显示YouTubePlayer

本文介绍了如何在Android的ListView中显示YouTubePlayerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下显示错误:

请检查一下.截至目前,传递单个videoID的问题一直停留在NullPointerException上.以下是我的VideoAdapter:

Please check it out. Passing single videoID as of now stuck with NullPointerException. Following is my VideoAdapter:

public class VideoAdapter extends BaseAdapter implements YouTubePlayer.OnInitializedListener {

    public static final String API_KEY = "Xxxx-pcD82qk8";
    public static final String VIDEO_ID = "7UpbJKeiv_Y";
    Context context;
    LayoutInflater inflater;
    JSONArray jArray = null;
    ArrayList<YouTubeItem> list;
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;

    public VideoAdapter(Activity context,
                        JSONArray jArray) {
        // TODO Auto-generated constructor stub

        parseResponse(jArray);
    }

    private void parseResponse(JSONArray jArray) {
        if (jArray != null && jArray.length() > 0) {
            String date;
            String title;
            String link;
            JSONObject jsonObject;
            list = new ArrayList<YouTubeItem>();

            for (int i = 0; i < jArray.length(); i++) {
                try {
                    jsonObject = jArray.getJSONObject(i);
                    date = jsonObject.getString("date");
                    title = jsonObject.getString("title");
                    link = jsonObject.getString("link");

                    if (date != null && title != null && link != null) {
                        list.add(new YouTubeItem(date, title, link));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }
        String[] date = new String[jArray.length()];
        String[] title = new String[jArray.length()];
        String[] link = new String[jArray.length()];

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.i("adapter 1", "");

        View rowView = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.youtube_view, parent, false);

        }
        Log.i("adapter 3", "");
        youTubePlayerView = (YouTubePlayerView) rowView
                .findViewById(R.id.youtubeplayerview);
        Log.i("adapter 4", "");
        youTubePlayerView.initialize(API_KEY, this);
        Log.i("adapter 5", "");
        TextView d = (TextView) rowView.findViewById(R.id.textView1);
        Log.i("adapter 6", "");
        TextView t = (TextView) rowView.findViewById(R.id.textView2);
        Log.i("adapter 7", "");
        TextView l = (TextView) rowView.findViewById(R.id.textView3);
        Log.i("adapter 8", "");
        d.setText(list.get(position).getDate());
        Log.i("adapter 9", "");
        t.setText(list.get(position).getTitle());
        Log.i("adapter 10", "");
        l.setText(list.get(position).getLink());
        Log.i("adapter 11", "");

        return rowView;
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }

    private class YouTubeItem {
        private String date;
        private String title;
        private String link;

        private YouTubeItem(String date, String title, String link) {
            this.date = date;
            this.title = title;
            this.link = link;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getLink() {
            return link;
        }

        public void setLink(String link) {
            this.link = link;
        }
    }

    @Override
    public void onInitializationFailure(Provider arg0,
                                        YouTubeInitializationResult arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onInitializationSuccess(Provider provider,
                                        YouTubePlayer player, boolean wasRestored) {
        // TODO Auto-generated method stub
        youTubePlayer = player;

        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);
        }
    }
}

推荐答案

用这个替换您Adapter

public class VideoAdapter extends BaseAdapter {

Context context;
LayoutInflater inflater;
JSONArray jArray = null;
ArrayList<YouTubeItem> list;

public VideoAdapter(YoutubeActivity youtubeActivity,
                    int activityYouTubeApi, JSONArray jArray) {
    // TODO Auto-generated constructor stub

    parseResponse(jArray);
}

private void parseResponse(JSONArray jArray) {
    if (jArray != null && jArray.length() > 0) {
        String date;
        String title;
        String link;
        JSONObject jsonObject;
        list = new ArrayList<>();

        for (int i = 0; i < jArray.length(); i++) {
            try {
                jsonObject = jArray.getJSONObject(i);
                date = jsonObject.getString("date");
                title = jsonObject.getString("title");
                link = jsonObject.getString("link");

                if (date != null && title != null && link != null) {
                    list.add(new YouTubeItem(date, title, link));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }
    String[] date = new String[jArray.length()];
    String[] title = new String[jArray.length()];
    String[] link = new String[jArray.length()];


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.youtube_view, parent, false);
    YouTubePlayerView you = (YouTubePlayerView) rowView.findViewById(R.id.youtubeplayerview);
    TextView d = (TextView) rowView.findViewById(R.id.textView1);
    TextView t = (TextView) rowView.findViewById(R.id.textView2);
    TextView l = (TextView) rowView.findViewById(R.id.textView3);

    d.setText(list.get(position).getDate());
    t.setText(list.get(position).getTitle());
    l.setText(list.get(position).getLink());

    return rowView;
}

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

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int i) {
    // TODO Auto-generated method stub
    return i;
}

private class YouTubeItem {
    private String date;
    private String title;
    private String link;

    private YouTubeItem(String date, String title, String link) {
        this.date = date;
        this.title = title;
        this.link = link;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
}
}

我创建了一个名为YouTubeItem的对象,而不是创建三个不同的数组.这将更易读,更易于操作,如果丢失了三个数组中任何一个的属性并进行单独的解析(无效对象不会影响整个流程),则可以避免任何问题.

Instead of creating three different Arrays, I created an Object called YouTubeItem. This will be more readable and easier to manipulate, avoid any problems if you have lost one attribute of any of the three arrays and make the parsing individual process (Invalid object will not affect the whole process)

如果您还有其他问题,请告诉我

Let me know if you have any more questions

这篇关于如何在Android的ListView中显示YouTubePlayerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:26