ListView单元素删除报错:Uri.getScheme()空指针异常求助
Hey there! Let's break down this NullPointerException you're hitting when trying to delete a ListView item via an AlertDialog. The error java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference makes it clear that somewhere in your Fragment's delete logic, you're calling getScheme() on a Uri that hasn't been initialized (it's null). Let's walk through how to fix this:
Step 1: Pinpoint where the null Uri is coming from
First, trace through your code to find exactly where you're working with Uri during the delete flow. Common culprits include:
- Not correctly fetching the selected list item (so the item itself is null, and any Uri tied to it is also null)
- Building a Uri using a null value (like a missing item ID when constructing a ContentProvider Uri)
- Losing the reference to the item between the long press event and the AlertDialog's confirm action
Step 2: Fix the item selection in the long press listener
Make sure you're reliably grabbing the selected item when the user long presses, and that it's not null before proceeding to the dialog:
listView.setOnItemLongClickListener((parent, view, position, id) -> { // Fetch the selected item - cast to your data model class YourDataModel selectedItem = (YourDataModel) parent.getItemAtPosition(position); // Guard against null items immediately if (selectedItem == null) { Log.e("YourFragmentTag", "Selected list item is null - can't proceed with delete"); return true; } // Pass the valid item to your delete dialog showDeleteConfirmationDialog(selectedItem); return true; });
Step 3: Ensure the Uri is valid in your delete method
If you're using a ContentProvider to delete data, double-check that you're building the Uri with valid, non-null parameters. For example:
private void deleteListItem(YourDataModel item) { // Verify the item has a valid ID (or whatever data you need to build the Uri) if (item.getId() == null) { Log.e("YourFragmentTag", "Item has no valid ID - can't build delete Uri"); return; } // Build the Uri safely using the item's ID Uri deleteUri = ContentUris.withAppendedId(YourContentProvider.CONTENT_URI, item.getId()); // Now execute the delete - the Uri is guaranteed non-null here getContext().getContentResolver().delete(deleteUri, null, null); // Update your ListView's adapter to reflect the deletion yourAdapter.remove(item); yourAdapter.notifyDataSetChanged(); }
Step 4: Guard against null in the AlertDialog's confirm action
Even if you passed a valid item to the dialog, it's safe to add a quick check before calling the delete method:
private void showDeleteConfirmationDialog(YourDataModel item) { new AlertDialog.Builder(getContext()) .setTitle("Delete Item") .setMessage("Are you sure you want to remove this item?") .setPositiveButton("Delete", (dialog, which) -> { if (item != null) { deleteListItem(item); } else { Log.e("YourFragmentTag", "Item reference lost - delete aborted"); } }) .setNegativeButton("Cancel", null) .show(); }
Bonus: Add debug logs
To make troubleshooting easier in the future, add log statements at key points (like after fetching the item, before building the Uri) to print values and confirm they're non-null. This will help you catch issues early.
内容的提问来源于stack exchange,提问作者Sobhy Elbhwashy

