Unable create alertDialog in ActionBarActivity
S

7

7

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"
        >
Soekarno answered 6/7, 2015 at 8:43 Comment(2)
possible duplicate of You need to use a Theme.AppCompat theme (or descendant) with this activityDisplant
You don't need to use android:theme="@style/Theme.AppCompat.NoActionBar" simply use android:theme="@style/AppTheme"Wrightson
D
27

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.

Dated answered 6/7, 2015 at 8:56 Comment(1)
Happy to hear so! You could also read this post to get a better understanding on when to use Activity or Application contextDated
F
10

I solved this issue by changing the import :

 android.support.v7.app.AlertDialog 

 to 

 android.app.AlertDialog
Fluecure answered 19/8, 2016 at 8:11 Comment(0)
S
2

Change

new AlertDialog.Builder(this)

to

new AlertDialog.Builder(MainActivity.this)
Swaraj answered 10/3, 2017 at 14:27 Comment(0)
L
1

If you are using alertDialog in adapter and in OnBindViewHolder() method as shown in below code:

@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 new AlertDialog.Builder(context) is not works properly

so You need to replace it with new AlertDialog.Builder(view.getContext()). This works for me,ty.

Lacrosse answered 12/6, 2017 at 11:26 Comment(0)
N
1

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

Nationality answered 18/7, 2017 at 17:0 Comment(0)
R
0

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

android:theme="@style/Theme.AppCompat.Light"
Recent answered 8/10, 2017 at 14:1 Comment(0)
D
-1

Use AppCompatActivity instead of ActionBarActivity

public class MyActivity extends AppCompatActivity
Dominquedominquez answered 6/7, 2015 at 8:47 Comment(1)
It doesn't change anything :-(Soekarno

© 2022 - 2024 — McMap. All rights reserved.