-3

This has been asked so many times, and has been answered,I have solved this problem before, but stuck this time, This is working in one place but in another activity its giving me following error message.

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

Following is the code of my registration class, the same class is written for login activity, and there its working perfectly.

public class RegistrationActivity extends AppCompatActivity 

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);


        if(sql_code.equalsIgnoreCase("0")){
            String resultCode= command1.getString("result");
            if(resultCode.equalsIgnoreCase("0")){
                AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
                alertDialog.setTitle("Account Created");
                alertDialog.setMessage("Account Created Successfully.");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i= new Intent(RegistrationActivity.this, LoginActivity.class);
                        startActivity(i);
                    }
                });

                alertDialog.show(); 

Manifest File

    <activity
        android:name=".ticketing.activities.checkout.RegistrationActivity"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_registration"
        android:theme="@style/AppTheme" />

Style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily">Roboto</item>
</style>

Everything is in place, but still its giving me this error message, Kindly guide me what i am doing wrong here.

dev90
  • 7,187
  • 15
  • 80
  • 153
  • 1
    Can you try changing "AppCompatActivity" to "Activity" ? – Sunil Sunny Aug 12 '16 at 10:51
  • Possible duplicate of [Android Custom Dialog](http://stackoverflow.com/questions/5544405/android-custom-dialog) – Anand Savjani Aug 12 '16 at 10:56
  • Possible duplicate http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – Sunil Sunny Aug 12 '16 at 10:58
  • I have already seen these question, but they did not help me much thats why i posted this question. – dev90 Aug 12 '16 at 10:59
  • @sunilsunny thanks, I have tried your solution but the problem is still there. – dev90 Aug 12 '16 at 10:59
  • 2
    Possible duplicate of [Android exception: You need to use a Theme.AppCompat theme with this activity](http://stackoverflow.com/questions/18764612/android-exception-you-need-to-use-a-theme-appcompat-theme-with-this-activity) – ste-fu Aug 12 '16 at 11:03
  • 1
    Check if you only have one style.xml. May be it is getting overwritten like in link given by @ste-fu – Sunil Sunny Aug 12 '16 at 11:09

5 Answers5

1
//instead of using it
 AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
//use It
AlertDialog alertDialog = new AlertDialog.Builder(RegistrationActivity.this).create();
shahid17june
  • 1,441
  • 1
  • 10
  • 15
  • And what is the difference ? – Sunil Sunny Aug 12 '16 at 10:55
  • read this so you can get what is exactly diffrence b/w them http://stackoverflow.com/questions/22966601/what-is-different-between-mainactivity-this-vs-getapplicationcontext – shahid17june Aug 12 '16 at 10:59
  • @sunilsunny Dialogs need activity context and not application context. Though usually the exception would be different in case app context was used. The question is not showing whether OP is using support lib or bare platform AlertDialog. – laalto Aug 12 '16 at 11:00
  • I am using `import android.support.v7.app.AlertDialog;` – dev90 Aug 12 '16 at 11:01
  • please accept the answer if its correct, so other people can also take help. – shahid17june Aug 12 '16 at 11:02
  • Thanks , The same thing is working is `Login Class`, but its not working here – dev90 Aug 12 '16 at 11:08
  • @ Kirmani88, i have tried the same code its run perfectly,can you please send me Logcat error. – shahid17june Aug 12 '16 at 11:13
1

Use this builder to create your dialog:

new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Dialog))

EDIT:

Another way to fix the problem is to create a custom style:

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">_your_color_</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">_your_color_</item>
<!-- Used for the background -->
<item name="android:background">_your_color_</item>

And then use it when building your dialog:

new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.MyAlertDialogStyle))
Seishin
  • 1,493
  • 1
  • 19
  • 30
1

Replace your dialog creation with the first line from this code

AlertDialog.Builder alertDialog= new AlertDialog.Builder(context);
                alertDialog.setTitle("Account Created");
                alertDialog.setMessage("Account Created Successfully.");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i= new Intent(RegistrationActivity.this, LoginActivity.class);
                        startActivity(i);
                    }
                });

                alertDialog.show(); 
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • That Worked Perfectly fine! Thanks :) It would be more awesome, if you explain why its working here. – dev90 Aug 12 '16 at 14:10
  • Although `alertDialog.setButton`, is not working in this code, I have to removed that in order to make it work. – dev90 Aug 12 '16 at 14:12
  • I haven't find the real problem right now but I faced the same issue few days ago, therefore I answered – Vivek Mishra Aug 12 '16 at 17:59
0

Change extends AppCompatActivity to extends Activity.

As you can see here - https://stackoverflow.com/a/21815015/4425195

Community
  • 1
  • 1
-1

Use DialogFragment for dialogs

Vygintas B
  • 1,624
  • 13
  • 31