Failed to create alertDialog in ActionBarActivity

I have an Activity that continues with an ActionBarActivity . Whenever I try to create an AlertDialog in it, a crash occurs in the line where a dialog is created with this error

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

But I already use the Appcompat theme Theme.AppCompat.Light.NoActionBar , as I use the toolbar. What could be the reason for this? Here is my activity:

 import android.app.ProgressDialog; import android.content.DialogInterface; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MyActivity extends ActionBarActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_comments); toolbar = (Toolbar)findViewById(R.id.tool_bar_comment); setSupportActionBar(toolbar); AlertDialog alertDialog; alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); alertDialog.setTitle("Network error"); alertDialog.setMessage("Check network connection and try again."); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); } }); alertDialog.show(); } } 

Here is my mainfest file:

  <application android:name=".Networking.AppController" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > ... <activity android:name=".MyActivity" android:label="@string/myactivity" > </activity> </application> 

and here styles.xml

 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> </style> 

Adding the android:theme attribute to activity in MainFest didn't help at all.

 <activity android:name=".MyActivity" android:label="@string/myactivity" android:theme="@style/Theme.AppCompat.NoActionBar" > 
+7
android appcompat alertdialog android-theme android-actionbar-compat
source share
7 answers

I could not reproduce the exact same error. However, I think the problem lies in the context passed to the AlertDialog.Builder constructor. In fact, the context should be conveyed to him.

Try replacing this line.

  alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); 

with this

  alertDialog = new AlertDialog.Builder(this).create(); 

Please let me know if this solves the problem.

+26
source share

I solved this problem by changing the import:

  android.support.v7.app.AlertDialog to android.app.AlertDialog 
+10
source share

Edit

 new AlertDialog.Builder(this) 

to

 new AlertDialog.Builder(MainActivity.this) 
+2
source share

If you use alertDialog in the adapter and in the OnBindViewHolder () method, as shown below:

 @Override public void onBindViewHolder(final UserListAdapter.ViewHolder holder, final int position) { //long click for delete the record holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { new AlertDialog.Builder(mContext) .setMessage("Are you want to delete?") .setPositiveButton("Yes",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Log.d(TAG, "onClick: yes"); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Log.d(TAG, "onClick: no"); } }) .setCancelable(false) .show(); return true; } }); 

}

In this case, the new AlertDialog.Builder (context) does not work correctly

so you need to replace it with the new AlertDialog.Builder (view.getContext ()) . It works for me, me.

+1
source share

It can be resolved by passing. Activityname.this instead of getApplicationContext(); as in your case alertDialog = new AlertDialog.Builder(MyActivity.this)).create();

0
source share

Add this line to your <application> in the AndroidManifest.xml file

 android:theme="@style/Theme.AppCompat.Light" 
0
source share

Use AppCompatActivity instead of ActionBarActivity

 public class MyActivity extends AppCompatActivity 
-one
source share

All Articles