Kirmani88 Kirmani88 - 3 months ago 20
Android Question

Alert Dialog crashing the application

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.

Answer

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(); 
Comments