为何Bundle出现空对象引用?Android列表编辑弹窗问题
Let's break down what's causing this crash and walk through how to fix it step by step.
Why You're Seeing This Error
The NullPointerException happens because getArguments() is returning null when you try to call getSerializable("city"). This means your EditCityFragment doesn't have any arguments attached to it when the dialog is created. Let's look at the specific issues in your code:
1. Wrong Return Type in newInstance Method
Your newInstance method is incorrectly returning an AddCityFragment instead of EditCityFragment. This is a copy-paste mistake—when you use this method to create a fragment instance, you're actually getting an AddCityFragment object, not the EditCityFragment you need. Even if you tried to pass the city data, it wouldn't be attached to the correct fragment.
2. Unnecessary Line of Code
In onCreateDialog, you have this useless line:
AddCityFragment.newInstance(savedInstanceState);
This creates an unused AddCityFragment instance and does nothing to set up your current EditCityFragment's arguments. It's just cluttering your code and doesn't help at all.
Step-by-Step Fixes
1. Correct the newInstance Method
Update the method to return an EditCityFragment instance (matching the class it belongs to):
static EditCityFragment newInstance(City city){ EditCityFragment fragment = new EditCityFragment(); Bundle args = new Bundle(); args.putSerializable("city", city); fragment.setArguments(args); return fragment; }
2. Remove the Unnecessary Code
Delete the line AddCityFragment.newInstance(savedInstanceState); from onCreateDialog—it serves no purpose and can be safely removed.
3. Ensure You're Using the Correct Fragment Creation Flow
When you trigger the dialog from your ListView click, make sure you use the corrected newInstance method to create the EditCityFragment, not a direct new EditCityFragment() call. For example:
// Inside your ListView click listener City selectedCity = yourCityList.get(position); // Get the clicked city EditCityFragment editDialog = EditCityFragment.newInstance(selectedCity); editDialog.show(getSupportFragmentManager(), "edit_city");
4. Add Null Safety (Optional but Recommended)
To prevent future crashes if arguments are ever missing, add null checks before accessing the city data:
@NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){ View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_city_fragment_layout, null); cityName = view.findViewById(R.id.city_name_editText); provinceName = view.findViewById(R.id.province_editText); Bundle args = getArguments(); if (args != null) { city = (City) args.getSerializable("city"); if (city != null) { cityName.setText(city.getCityName()); provinceName.setText(city.getProvinceName()); } } AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); return builder .setView(view) .setTitle("Edit City") .setNegativeButton("Cancel", null) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String city = cityName.getText().toString(); String province = provinceName.getText().toString(); listener.onOkPressed(new City(city, province)); } }).create(); }
Final Notes
The core issue was a mismatched fragment type in your newInstance method, combined with a useless line of code that didn't contribute to setting up your fragment's arguments. Fixing these two things will ensure your city data is properly passed to the EditCityFragment, eliminating the NullPointerException.
内容的提问来源于stack exchange,提问作者tappily

