In the class public class FragmentMyIngredients extends Fragment there is an AlertDialog.Builder deleteDialog;

Which is initialized in this method.

 private void createDeleteDialog() { deleteDialog = new AlertDialog.Builder(getActivity().getBaseContext()); deleteDialog.setTitle("Вы действительно хотите удалить выбранные ингредиенты?"); deleteDialog.setPositiveButton("Да", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { deleteIngredients(); } }); deleteDialog.setNegativeButton("Нет", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); deleteDialog.setCancelable(true); deleteDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialogInterface) { } }); } 

It is called when you click on a menu item with the show() method.

As a result, the java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. error occurs java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

What needs to be fixed? I, of course, understand that in the description of the error it is said to use a specific topic, but having tried several options, I did not come to success.

  • From which is Activation inherited in which fragment? - Eugene Zaychenko
  • @EugeneZaychenko here is the full class declaration: public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener - mtrfnv

1 answer 1

Change the line:

 deleteDialog = new AlertDialog.Builder(getActivity().getBaseContext()); 

on:

 deleteDialog = new AlertDialog.Builder(getActivity()); 
  • thanks, earned. however, I initially used deleteDialog = new AlertDialog.Builder (getActivity (). getApplicationContext ()); but during the experiments I changed it to what I wrote in the question - mtrfnv
  • one
    getActivity (). getApplicationContext () is also a bad option, you do not need getApplicationContext () at all, the activation will provide the necessary context. The higher the parent of the context you use, the worse. - Eugene Zaychenko
  • possiblemobile.com/2013/06/context here about the context - Eugene Zaychenko