From 2e7234ce1d0affd1835233fabcb5b351abe332f2 Mon Sep 17 00:00:00 2001 From: Ben Lin Date: Thu, 14 Jul 2016 10:40:00 -0700 Subject: [PATCH] Fix a crash happening on a context menu. When an activity gets a callback for onContextItemSelected, it first gets dibs on handling it, and then it propagates the call to -every- fragment it is tied to. This poses a problem since RootsFragment and DirectoryFragment don't necessarily handle each other's menu items well, which can cause NPEs. This is some temporary fix for it. Bug: 30092676 Change-Id: I3f268c9c9580504b78581ad92a24c9a99c4a0daf (cherry picked from commit 3e1699f3a3f3d0378d758e3b0d430165867eb89e) --- .../src/com/android/documentsui/RootsFragment.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java b/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java index f381bb20222e2..ca28622327c87 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java @@ -298,8 +298,15 @@ public class RootsFragment extends Fragment implements ItemDragListener.DragHost @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); + // There is a possibility that this is called from DirectoryFragment since + // all fragments' onContextItemSelected gets called when any menu item is selected + // This is to guard against it since DirectoryFragment's RecylerView does not have a + // menuInfo + if (adapterMenuInfo == null) { + return false; + } final RootItem rootItem = (RootItem) mAdapter.getItem(adapterMenuInfo.position); - switch(item.getItemId()) { + switch (item.getItemId()) { case R.id.menu_eject_root: final View ejectIcon = adapterMenuInfo.targetView.findViewById(R.id.eject_icon); ejectClicked(ejectIcon, rootItem.root);