以下显示错误:
NullPointerException:无法启动活动组件(a.b.YoutubeActivity)
请检查出来。到目前为止,传递单个videoID仍然存在NullPointerException。以下是我的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);
}
}
}
答案 0 :(得分:1)
用这个替换你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
的对象,而不是创建三个不同的数组。这将更易读,更容易操作,如果丢失了三个数组中任何一个数据的一个属性,并且解析单个进程(无效对象不会影响整个过程),则可以避免任何问题。
如果您还有其他问题,请与我们联系