[Android] Prevent NPE when medialist is null

Geoffrey Métais git at videolan.org
Wed Mar 4 13:27:41 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar  4 13:17:56 2015 +0100| [a650c59dfdbd87399db9743c8ee06bf56ff76947] | committer: Geoffrey Métais

Prevent NPE when medialist is null

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=a650c59dfdbd87399db9743c8ee06bf56ff76947
---

 .../src/org/videolan/vlc/gui/video/VideoGridFragment.java    |    8 +++++++-
 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java     |   10 ++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 214f8c1..51be6ab 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -269,6 +269,8 @@ public class VideoGridFragment extends BrowserFragment implements ISortable, IVi
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         MediaWrapper media = mVideoAdapter.getItem(position);
+        if (media == null)
+            return;
         if (media instanceof MediaGroup) {
             MainActivity activity = (MainActivity)getActivity();
             VideoGridFragment frag = (VideoGridFragment)activity.showSecondaryFragment("videoGroupList");
@@ -290,6 +292,8 @@ public class VideoGridFragment extends BrowserFragment implements ISortable, IVi
 
     private boolean handleContextItemSelected(MenuItem menu, int position) {
         MediaWrapper media = mVideoAdapter.getItem(position);
+        if (media == null)
+            return false;
         switch (menu.getItemId())
         {
         case R.id.video_list_play_from_start:
@@ -329,7 +333,7 @@ public class VideoGridFragment extends BrowserFragment implements ISortable, IVi
         // Do not show the menu of media group.
         AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
         MediaWrapper media = mVideoAdapter.getItem(info.position);
-        if (media instanceof MediaGroup)
+        if (media == null || media instanceof MediaGroup)
             return;
         MenuInflater inflater = getActivity().getMenuInflater();
         inflater.inflate(R.menu.video_list, menu);
@@ -371,6 +375,8 @@ public class VideoGridFragment extends BrowserFragment implements ISortable, IVi
         PopupMenu popupMenu = new PopupMenu(getActivity(), anchor);
         popupMenu.getMenuInflater().inflate(R.menu.video_list, popupMenu.getMenu());
         MediaWrapper media = mVideoAdapter.getItem(position);
+        if (media == null)
+            return;
         setContextMenuItems(popupMenu.getMenu(), media);
         popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index ed7d096..5c54423 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -35,6 +35,7 @@ import org.videolan.vlc.util.Util;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.graphics.Bitmap;
+import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -258,4 +259,13 @@ public class VideoListAdapter extends ArrayAdapter<MediaWrapper>
     public boolean isListMode() {
         return mListMode;
     }
+
+    @Override
+    @Nullable
+    public MediaWrapper getItem(int position) {
+        if (position < getCount() && position > 0)
+            return super.getItem(position);
+        else
+            return null;
+    }
 }



More information about the Android mailing list