I have
public static class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Would you like to share your contacts?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
readContacts task = new readContacts(); // The IDE doesn't like "new readContacts()"
task.execute();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
But the IDE says 'com.example.MainActivity.this' cannot be referenced from a static context
. But when I remove the static
from FireMissilesDialogFragment
declaration it says that FireMissilesDialogFragment
should be static
. So, isn't there any method to execute an AsyncTask
within DialogFragment
?
Edit: Here is my readContacts
:
public class readContacts extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... voids){
...
}
public class readContacts extends AsyncTask<Void, Void, Void> {
is an inner class of public class MainActivity extends AppCompatActivity {
.