Question

i have this code in java:

public class AboutFragment extends Fragment {

public AboutFragment(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_about, container, false);

    new AlertDialog.Builder(this)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue 
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
    .setIcon(R.drawable.ic_dialog_alert)
     .show();

    return rootView;
}

and error is: The constructor AlertDialog.Builder(AboutFragment) is undefined, ic_dialog_alert cannot be resolved or is not a field

Was it helpful?

Solution

That's because the AlertDialog.Builder requires a Context and a Fragment is not a Context. Try getActivity() instead of this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top