Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
        >
like image 465
Prathmesh Deshmukh Avatar asked Jul 06 '15 08:07

Prathmesh Deshmukh


People also ask

How do I use AlertDialog builder?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.


1 Answers

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.

like image 61
Ajeje Avatar answered Sep 23 '22 02:09

Ajeje