用于ListView的Android自定义ArrayAdapter,其中YouTubeAndroidPlayerApi给出NullPointerException

时间:2016-04-12 13:15:27

标签: listview youtube-api android-arrayadapter

我在Google上搜索了很多,但似乎无法找到任何解决方案。

我正在使用YouTubeAndroidPlayerApi。我正在尝试在ListView中显示YouTube视频列表。

我有一个片段类,如下所示 -

GalleryFragment.java

public class GalleryFragment  extends Fragment{
ProgressDialog pd;
private static final String API_KEY = Config.DEVELOPER_KEY;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,         
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.gallery_layout,   
null);

Bundle bundle = this.getArguments();
int user_id = bundle.getInt("user_id", 0);

TextView txtNoGallery = (TextView) root.findViewById(R.id.txtNoGallery);
txtNoGallery.setVisibility(View.INVISIBLE);

String strUrl = getString(R.string.saltchip_gallery_url)+"?
user_id="+user_id;

if(user_id!=0) {
if (connectionAvailable()) {
new GetDataFromServerTask().execute(strUrl);
} else
Toast.makeText(getActivity(), "No internet connection", 
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(),"Error Provessing. Please try again     
later",Toast.LENGTH_SHORT).show();
Intent objDash = new Intent(getActivity(),DashboardActivity.class);
startActivity(objDash);
}

return root;
}

public boolean connectionAvailable() {
boolean connected = false;

ConnectivityManager cm = (ConnectivityManager) 
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == 
NetworkInfo.State.CONNECTED ||
cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == 
NetworkInfo.State.CONNECTED) {
connected = true;
}

return connected;
}

public class GetDataFromServerTask extends AsyncTask<String, Void, 
String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(getActivity(), getString(R.string.app_name), 
"Fetching information....");
pd.setCancelable(false);
}

@Override
protected String doInBackground(String... params) {
api_service wa = new api_service();
try {
return wa.getData(params[0]);
} catch (Exception e) {
return e.toString();
}
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

pd.dismiss();

TextView txtNoGallery = (TextView) 
getActivity().findViewById(R.id.txtNoGallery);

try {
JSONArray fetchedResponse = new JSONArray(s);

JSONObject json_data;

if (fetchedResponse != null) {

List<Gallery> listGallery = new ArrayList<Gallery>();

for (int i = 0; i < fetchedResponse.length(); i++) {
json_data = null;
json_data = fetchedResponse.getJSONObject(i);
String gallery_item_type = json_data.getString("type");
String imgPath = null;
String vidPath = null;


if(gallery_item_type.equals("image"))
imgPath = json_data.getString("0");
else if(gallery_item_type.equals("video"))
vidPath = json_data.getString("0");

listGallery.add(new Gallery(imgPath,vidPath,gallery_item_type));
}


ListView grid = (ListView) getActivity().findViewById(R.id.gridGallery);
ListGallery adapter = new 
ListGallery(getContext(),R.layout.gallery_single,listGallery);
grid.setAdapter(adapter);

} else {
txtNoGallery.setVisibility(View.VISIBLE);
txtNoGallery.setText("There are not items in your Gallery");
}


} catch (JSONException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();                
Toast.makeText(getContext(), "Error. Please try again later", 
Toast.LENGTH_LONG).show();
}
}
}

}

我的适配器类如下 -

ListGallery.java

public class ListGallery extends ArrayAdapter<Gallery> {
public ListGallery(Context context, int resource, List<Gallery> objects) 
{
super(context, 0, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Gallery gallery = getItem(position);
try {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gallery_single,parent,false);
if(gallery.gallery_item_type.equals("image"))
{
FrameLayout youTubePlayerView = (FrameLayout) 
convertView.findViewById(R.id.vidGalleryItem);
youTubePlayerView.setVisibility(View.GONE);
ImageView imgGalleryItem = (ImageView) 
convertView.findViewById(R.id.imgGalleryItem);
imgGalleryItem.setVisibility(View.VISIBLE);
}
else if(gallery.gallery_item_type.equals("video"))
{
ImageView imgGalleryItem = (ImageView) 
convertView.findViewById(R.id.imgGalleryItem);
imgGalleryItem.setVisibility(View.GONE);

String strVideoUrl = gallery.vidPath;
String VIDEO_ID = getYoutubeVideoId(strVideoUrl);

VideoFragment videoFragment = VideoFragment.newInstance(VIDEO_ID);
}
}
catch (Exception e)
{
System.out.println("Get ViewException - "+e.getMessage());
e.printStackTrace();
}
return convertView;
}

public static String getYoutubeVideoId(String youtubeUrl)
{
String video_id="";
if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && 
youtubeUrl.startsWith("http"))
{
String expression = "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|
(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*
((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?
([^#\&\?]*).*/;
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches())
{
String groupIndex1 = matcher.group(7);
if(groupIndex1!=null && groupIndex1.length()==11)
video_id = groupIndex1;
}
}
return video_id;
}
}

我的布局如下 -

gallery_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/txtNoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ListView
android:id="@+id/gridGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

我的行布局项目文件如下 -

gallery_single.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imgGalleryItem"
android:layout_width="150dip"
android:layout_height="150dip"
android:src="@drawable/default_dp"
android:scaleType="fitCenter"/>

<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragm
ent"
android:id="@+id/vidGalleryItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

但是在加载片段时,我得到一个黑色的黑色屏幕,在滚动时会出现以下错误 -

  

获取ViewException - 二进制XML文件行#33:错误膨胀类片段   android.view.InflateException:二进制XML文件行#33:错误膨胀类片段

     

在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)

     

在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)

     

在android.view.LayoutInflater.inflate(LayoutInflater.java:489)

     

在android.view.LayoutInflater.inflate(LayoutInflater.java:396)

     

at com.mubin.khalife.saltchip.ListGallery.getView(ListGallery.java:32)

     

在android.widget.AbsListView.obtainView(AbsListView.java:2159)

     

在android.widget.ListView.makeAndAddView(ListView.java:1831)

     

在android.widget.ListView.fillDown(ListView.java:674)

     

在android.widget.ListView.fillGap(ListView.java:638)

     

在android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)

     

在android.widget.AbsListView.onGenericMotionEvent(AbsListView.java:3640)

     

在android.view.View.dispatchGenericMotionEventInternal(View.java:7341)

     

在android.view.View.dispatchGenericMotionEvent(View.java:7322)   在android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782)

     

在android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735)

     

在android.view.View.dispatchGenericMotionEvent(View.java:7315)

     

在android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782)

     

在android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735)

     

在android.view.View.dispatchGenericMotionEvent(View.java:7315)

0 个答案:

没有答案