I have an Activity
which extends ActionBarActivity
. Whenever I try to create an AlertDialog
in it, crashes at the the line where dialog is created giving this error
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
But I'm already using Appcompat
theme Theme.AppCompat.Light.NoActionBar
as I'm using 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 is the styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Adding the android:theme
attribute to the activity in MainFest didn't help at all.
<activity
android:name=".MyActivity"
android:label="@string/myactivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
>