Snippets must be static so that they can be re-created by the system, and anonymous classes are not static.

The following code shows me the following error:

"Fragments must be static so that they can be re-created by the system, and anonymous classes are not static."

How can i fix this?

public void A(){ final DialogFragment dialogFragment = new DialogFragment() { @Override public Dialog onCreateDialog(Bundle bundle) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage("Hello"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { B(); } } ); return builder.create(); } }; } private void B() { //... } 
+1
source share
1 answer

Read the fragment life cycle. You should use this as

 public static class MyAlertDialogFragment extends DialogFragment { public static MyAlertDialogFragment newInstance(int title) { MyAlertDialogFragment frag = new MyAlertDialogFragment(); Bundle args = new Bundle(); args.putInt("title", title); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.alert_dialog_icon) .setTitle(title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((FragmentAlertDialog)getActivity()).doPositiveClick(); } } ) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((FragmentAlertDialog)getActivity()).doNegativeClick(); } } ) .create(); } } 
+4
source

All Articles