Kirmani88 Kirmani88 - 4 months ago 30
Android Question

Error Message: You need to use a Theme.AppCompat theme with this activity

This question has been asked so many times, but still i am unable to solve this problem.

I am trying to show Alert windows in my activity. Its giving me following error message


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


This is my code.

public class CartActivity extends AppCompatActivity

btnCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(cbApprove.isChecked()){

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setTitle("Thanks");
alertDialog.setMessage("Thanks for participating in ticketing flow. The test ends here for the Beta");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});

alertDialog.show();

}
}
});


Manifest File

<activity
android:name=".ticketing.activities.CartActivity"
android:label="@string/title_activity_cart"
android:theme="@style/AppTheme"

></activity>


Style v21

<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>
</style>


I have also changed the style of layout to AppComat.NoActionBar
from Layout Design
.

Error Message

FATAL EXCEPTION: main
Process: com.tixsee.mavs, PID: 15867
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:340)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80)
at android.support.v7.app.AlertController.installContent(AlertController.java:214)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:256)
at android.app.Dialog.dispatchOnCreate(Dialog.java:463)
at android.app.Dialog.show(Dialog.java:288)
at com.tixsee.mavs.ticketing.activities.CartActivity$1.onClick(CartActivity.java:92)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)

Answer

I guess the error is caused because of using AlertDialog in your code. You need to pass your activity's context instead of the application context to the alertdialog builder. Get the context in your AlertDialog Builder using getActivity() instead of using getApplicationContext(). Like this:

AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();