How To Display Youtubeplayerview In A Listview In Android?
The following shows error: NullPointerException: Cannot start activity component(a.b.YoutubeActivity) Please check it out. Passing single videoID as of now stuck with NullPoin
Solution 1:
Replace you Adapter
with this one
publicclassVideoAdapterextendsBaseAdapter {
Context context;
LayoutInflater inflater;
JSONArray jArray = null;
ArrayList<YouTubeItem> list;
publicVideoAdapter(YoutubeActivity youtubeActivity,
int activityYouTubeApi, JSONArray jArray) {
// TODO Auto-generated constructor stubparseResponse(jArray);
}
privatevoidparseResponse(JSONArray jArray) {
if (jArray != null && jArray.length() > 0) {
String date;
String title;
String link;
JSONObject jsonObject;
list = newArrayList<>();
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(newYouTubeItem(date, title, link));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
String[] date = newString[jArray.length()];
String[] title = newString[jArray.length()];
String[] link = newString[jArray.length()];
}
@OverridepublicViewgetView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stubLayoutInflater 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;
}
@Overridepublic int getCount() {
return list.size();
}
@OverridepublicObjectgetItem(int arg0) {
// TODO Auto-generated method stubreturnnull;
}
@Overridepublic long getItemId(int i) {
// TODO Auto-generated method stubreturn i;
}
privateclassYouTubeItem {
privateString date;
privateString title;
privateString link;
privateYouTubeItem(String date, String title, String link) {
this.date = date;
this.title = title;
this.link = link;
}
publicStringgetDate() {
return date;
}
publicvoidsetDate(String date) {
this.date = date;
}
publicStringgetTitle() {
return title;
}
publicvoidsetTitle(String title) {
this.title = title;
}
publicStringgetLink() {
return link;
}
publicvoidsetLink(String link) {
this.link = link;
}
}
}
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
Post a Comment for "How To Display Youtubeplayerview In A Listview In Android?"