Java – provides an example of an incorrect alertdialog
I'm new to Java / Android Development (I started learning last night), so it's entirely possible for me to do something very stupid. However, after more than an hour of Google search, I didn't think of anything. I'm using eclipse as my editor
I am reading the alertdialog document here, 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 initially rewritten it, so I can start submitting some methods to memory, but I get an error "fragmentalertdialog cannot be resolved to type". I click Ctrl Shift o to make sure I have the correct import, but it still doesn't disappear
So I copied / pasted the sample code and did the following in the following order:
>Press Ctrl Shift O and right-click import (use android.app.dialogfragment instead of android.support.v4.app.dialogfragment) > declare my package at the top > replace r.string.alert with android.r.string.ok and android.r.string.cancel, respectively_ dialog_ OK and r.string.alert_ dialog_ Cancel > deleted seticon () because I don't have an icon to put in yet
I'm still getting errors:
>Fragmentalertdialog cannot be resolved to illegal modifier of type (x4) > myalertdialogfragment class; Only public, abstract and final are allowed
Did I do something wrong, or was there a problem with the sample code?
resolvent:
Make sure that the activity you want to convert to is named fragmentalertdialog. Make sure that you also save everything - sometimes eclipse won't establish a connection until you save everything
Remove the static modifier:
public class MyAlertDialogFragment extends DialogFragment {
Or keep static and move this fragment to include it in the activity you want. This means that myalertdialogfragment should precede the closing bracket of the activity
Don't start with such a complex thing. Learn Java and then go to Android