0

A beginner here..

I'm unable to create alert dialog. It occurs when I use `alertDialog.show();

this error--

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

my code,

 logoImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
            builder.setTitle("hello");
            builder.setMessage("hello how are you");
            AlertDialog alertDialog= builder.create();
            alertDialog.show(); //line 


        }

    });

my logcat..

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
    at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)
    at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:94)
    at android.support.v7.app.AlertController.installContent(AlertController.java:232)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:279)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
    at android.app.Dialog.show(Dialog.java:283)
    at com.abmm.include_activity$15.onClick(include_activity.java:459)

thank you buddies..

Monk
  • 57
  • 1
  • 10
  • possible duplicate https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – Ashish singh Jul 12 '19 at 09:13
  • @Ashishsingh I read this did not help me.. also I'm not finding any explanation about what they are doing and why? – Monk Jul 12 '19 at 09:35
  • @Monk in your activity class from where you are calling dialog ,extend from Activity instead of AppcompatActivity . – Ashish singh Jul 12 '19 at 09:46
  • its done buddy @Ashishsingh its the error of context.. I declared the context globally Context mycontext=this; and then AlertDialog.Builder builder = new AlertDialog.Builder(mycontext); thanks for ur support ;-) – Monk Jul 12 '19 at 09:59

1 Answers1

0

Change,

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

to

AlertDialog.Builder builder = new AlertDialog.Builder(this);
Hardik Chauhan
  • 2,750
  • 15
  • 30
  • `this` won't work since the Alert is inside a click listener. Make a reference to the activity context instead. – Zun Jul 12 '19 at 09:15
  • if you are calling this from activity than pass that context like, MainActivity.this or if this is fragment than getActivity() – Hardik Chauhan Jul 12 '19 at 09:16
  • Hi @Zun I tried it like AlertDialog.Builder builder = new AlertDialog.Builder(include_Activity.this); Same error is giving.. please help – Monk Jul 12 '19 at 09:23
  • @HardikChauhan hiii......I have done this buddy , dont know why its giving same error..help pls – Monk Jul 12 '19 at 09:24
  • are you calling this dialog from activity or fragment? – Hardik Chauhan Jul 12 '19 at 09:29
  • what theme you are using in your manifest file? – Hardik Chauhan Jul 12 '19 at 09:33
  • @HardikChauhan from activity..i dont have any fragment – Monk Jul 12 '19 at 09:53
  • 1
    @HardikChauhan its done... I declared Context mycontext= this; in the top of class and then AlertDialog.Builder builder = new AlertDialog.Builder(mycontext); it solved the error..thanks for ur support too... – Monk Jul 12 '19 at 09:55