Android - 不是封闭的类

时间:2015-12-30 10:04:09

标签: java android

我知道这个问题已被提出但我无法解决。我想在Android的服务上显示简单的AlertDialog。我可以在MainActivity上很好地展示它,但我在Service上遇到了问题,这是我的代码:

CustomMainActivity.java:

public void popupDialogMain()
{
    final Context context = getApplicationContext();
    Handler h1 = new Handler(context.getMainLooper());
    h1.post(new Runnable() {
        @Override
        public void run() {
            if (mBXmpp)
                mBXmppService.popupDialogMain2();
        }
    });
}

XmppService.java:

public static void popupDialogMain2()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)
           .setMessage("Look at this dialog!")
           .setCancelable(true)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    //do things
               }
           });
     AlertDialog alert = builder.create();
     builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
     alert.show();
}

我在这一行收到错误: AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)

Android Manifest: 我添加了这个权限:

android.permission.SYSTEM_ALERT_WINDOW

我收到此错误: 不是封闭类:CustomMainActivity

有什么建议可以解决吗?

1 个答案:

答案 0 :(得分:2)

您无法从静态方法访问this