Search code examples
androidandroid-alertdialogandroid-appcompatandroid-themeandroid-actionbar-compat

Unable create alertDialog in ActionBarActivity


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"
        >

Solution

  • I couldn't reproduce the same exact error. However, I think that the problem is the context passed to AlertDialog.Builder constructor. In fact, an activity Context should be passed to it.

    Try replacing this line

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

    with this one

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

    Please let me know if this solves the problem.