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
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> </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" >
android appcompat alertdialog android-theme android-actionbar-compat
Prathmesh deshmukh
source share