-1

I'm trying to initialize the activityManager like this but it failed . here is the code :

private ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

Can someone tell me why ??

4

1 回答 1

0

private建议您尝试初始化成员变量。Activity.getSystemService()需要 a 正确初始化Context,您不能将 aActivity用作Contextuntil onCreate()

这也在您的问题标题中说明为您得到的例外,IllegalStateException并带有有用的消息"System services not available to Activities before onCreate()"

要修复它,只需将变量初始化移动到onCreate().

于 2013-11-05T12:27:12.933 回答