AlertDialog example giving errors

I'm new to Java / Android development (I started learning last night), so it's entirely possible that I'm doing something terribly stupid. However, after more than an hour of googling, I came up with nothing. I use Eclipse as my editor.

I read the docs here for AlertDialog , which gives an example:

 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(); } } 

I originally rewrote it, so I can start using some of the methods in memory, but I got the error "FragmentAlertDialog cannot be resolved to type". I pressed Ctrl + Shift + O to make sure that I have the correct import, but still it has not disappeared.

So, I copied / pasted the example code and did the following in the following order:

  • Press Ctrl + Shift + O to get import rights (using android.app.DialogFragment , not android.support.v4.app.DialogFragment )
  • Announced my package at the top
  • Replaced R.string.alert_dialog_ok and R.string.alert_dialog_cancel with android.R.string.ok and android.R.string.cancel respectively
  • Removed setIcon() since I don't have an icon to add

I still get errors:

  • FragmentAlertDialog cannot be allowed for type (x4)
  • Invalid modifier for class MyAlertDialogFragment ; only public , abstract and final allowed

Am I doing something wrong or is something wrong with the sample code?

+4
source share
2 answers

1.FragmentAlertDialog

Make sure the action you want to pass is called FragmentAlertDialog . Be sure to save everything - sometimes Eclipse will not establish a connection until everything is saved.

2.Illegal modifier for the MyAlertDialogFragment class; only public, abstract and final are allowed

Take out the static modifier:

 public class MyAlertDialogFragment extends DialogFragment { 

or keep static and move this fragment so that it is enclosed in the desired action. This means that MyAlertDialogFragment must be inside your Activity before this command closes.

I am new to Java / Android development

Do not start with something so complicated. Learn Java , then go to Android.

+2
source

Hi, try this code to implement a warning dialog

  AlertDialog.Builder alert2 = new AlertDialog.Builder(this); alert2.setTitle("Your Title"); alert2.setMessage("Your Messages"); final EditText input2 = new EditText(this); input2.setInputType(InputType.TYPE_CLASS_PHONE); alert2.setView(input2); alert2.setPositiveButton(GButton, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do something with value! try { // do your stuff here } catch(Exception e) { } } }); alert2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert2.show(); 
+1
source

All Articles