简体   繁体   中英

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

There are some other articles with same subject, but my case is different.

I am using a BroadcastReciever in an AlarmManager application. Upon Alarm fire, I am trying to display an AlertDialog with custom layout. As the alarm gets fired, I get the following exception:

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

Although I am using Theme.AppCompat.Light.NoActionBar for my whole application. Here is the Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    >

I am specifying this style in my dialog's layout file as well:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#10000000"
        android:orientation="vertical"
        style="@style/Theme.AppCompat.Light.NoActionBar"
        android:padding="10dip" >
<TextView/>...
<Button/>...
</LinearLayout>

Furthermore I am opening the dialog form the onRecive() method of my BroadcastReciver class:

 @Override
    public void onReceive(Context context, Intent intent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inflater.inflate(R.layout.fragment_alarm_running, null);
        final AlertDialog dialog = new AlertDialog.Builder(context)
                .setView(v)
                .setCancelable(false)
                .create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(final DialogInterface dialog) {  ...   }
        });
        dialog.show();
} 

NOTE: The onRecieve() is getting called successfully, but the problem is with displaying dialog . Before I also tried a separate activity for displaying the dialog but the activity does not start from background when the app is closed.

How can I resolve this? Thanks in advance!

Broadcast receiver is the component of your application just like your Activity. You are trying to show Alert Dialog in receiver which required Theme.AppCompat, but your Broadcast receiver doesn't have that theme. What you need to do is open an activity on receiving broadcast and show dialog in it like the calling feature. OR use notification manager to show notification with Action buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM