我正在构建一个应用程序,用户可以在其中创建一个listView,然后用户可以单击其中一个listViews,然后将弹出一个对话框,让用户编辑他们单击的listView。 这是我的EditCityFragment,一旦用户单击listView,就会调用该片段。而且我正在使用一个arraylist来存储我的数据,我已经弄清楚了如何添加城市。
static AddCityFragment newInstance(City city){
AddCityFragment fragment = new AddCityFragment();
Bundle args = new Bundle();
args.putSerializable("city", city);
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_city_fragment_layout, null);
AddCityFragment.newInstance(savedInstanceState);
cityName = view.findViewById(R.id.city_name_editText);
provinceName = view.findViewById(R.id.province_editText);
city = (City) getArguments().getSerializable("city");
String s = city.getCityName();
String p = city.getProvinceName();
cityName.setText(s);
provinceName.setText(p);
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();
}
}
这是错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.simpleparadox.listycity, PID: 13929
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference
at com.example.simpleparadox.listycity.EditCityFragment.onCreateDialog(EditCityFragment.java:57)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)