Android 无法在我的类中启动扩展GcmListenerService的对话框

Android 无法在我的类中启动扩展GcmListenerService的对话框,android,android-activity,google-cloud-messaging,android-dialogfragment,android-dialog,Android,Android Activity,Google Cloud Messaging,Android Dialogfragment,Android Dialog,当我在我的类中收到扩展了GcmListenerService public class MyGcmListenerService extends GcmListenerService { @Override public void onMessageReceived(String from, Bundle data) { final String senderName = data.getString("sender"); final String message = dat

当我在我的类中收到扩展了
GcmListenerService

public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {

    final String senderName = data.getString("sender");
    final String message = data.getString("message");

    if(senderName != null && senderName.equals("XXX")){
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message)
                .setTitle("Alert!")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                        dialog.dismiss();
                    }
                });
        try {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    final AlertDialog alertDialog = builder.create();
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    alertDialog.show();
                    Looper.loop();

                }
            }.start();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
但我收到一个异常
java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或子代)。

在此行
final AlertDialog AlertDialog=builder.create()

我的主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar =(Toolbar) findViewById(R.id.mainActivityBar);
     setSupportActionBar(toolbar);
    //other statements and methods here
}
My manifest.xml

 <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/mainActivityTheme">
    </activity>

我的风格资源

<style name="mainActivityTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:windowActionBar">false</item>
</style>
public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
final String senderName = data.getString("sender");
final String message = data.getString("message");

if(senderName != null && senderName.equals("XXX")){
   final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.mainActivityTheme));

    builder.setMessage(message)
            .setTitle("Alert!")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                    dialog.dismiss();
                }
            });
    try {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                final AlertDialog alertDialog = builder.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
                Looper.loop();

            }
        }.start();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

假的
真的
@android:彩色/白色
假的

我哪里做错了?

我必须在活动中扩展
AppCompatActivity
,因为我想使用
setSupportActionBar()
方法将我的工具栏设置为操作栏

为了解决这个问题,我在类中做了一些更改,扩展了
GcmListenerService

public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {

    final String senderName = data.getString("sender");
    final String message = data.getString("message");

    if(senderName != null && senderName.equals("XXX")){
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message)
                .setTitle("Alert!")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                        dialog.dismiss();
                    }
                });
        try {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    final AlertDialog alertDialog = builder.create();
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    alertDialog.show();
                    Looper.loop();

                }
            }.start();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
我变了

final AlertDialog.Builder builder = new AlertDialog.Builder(this);

这是我扩展gcmlistener服务的班级

<style name="mainActivityTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:windowActionBar">false</item>
</style>
public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
final String senderName = data.getString("sender");
final String message = data.getString("message");

if(senderName != null && senderName.equals("XXX")){
   final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.mainActivityTheme));

    builder.setMessage(message)
            .setTitle("Alert!")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                    dialog.dismiss();
                }
            });
    try {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                final AlertDialog alertDialog = builder.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
                Looper.loop();

            }
        }.start();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
` 我的活动和AlertDialog.Builder使用的我的样式

<style name="mainActivityTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:windowActionBar">false</item>
</style>

假的
真的
@android:彩色/白色
假的

为什么要尝试使用AppCompatActivity(用于带有操作栏的活动)当你有一个NoActionBar主题时?@TimCastelijns因为我需要
设置支持ActionBar
,而我不能通过扩展Activity@TimCastelijns或者我有没有办法不扩展AppCompatActivity而
setSupportActionBar
正常情况下,当您想使用操作栏时,您使用的主题不是NoActionBar,查看名称,原因应该是显而易见的获得以下错误:“无法添加窗口android.view.ViewRootImpl$W@2728b31--此窗口类型的权限被拒绝“有任何建议吗?啊,明白了:酷!为我工作。。。需要补充的是:正如这家伙所说:@moehward我当时离你很远,但是当我回来的时候,我发现你解决了这个问题。伟大的如果这个答案对你有帮助,别忘了投票:)