应用程序中的静态变量class returns 访问时为null

Static variable in Application class returns null when accessing it

我在应用程序中定义一个变量 class 像这样

public static ResultReceiver myResultReceiver = null;

在某些活动中,我正在扩展 ResultReceiver 并分配给 myResultReceiver,在活动的 stop() 中,我将其设为空。 尽管当我从 intentservice 访问它时 myResultReceiver 还不是 null,但它是 null。直到今天,代码都运行良好。

可能是,只是可能是您使用 android:process or android:isolatedProcess 属性在另一个进程上提供服务。如果是这样,那将创建一个独立的新进程,服务在其中运行,并且不可能从另一个进程读取任何数据,甚至是静态变量,除非使用 IPCSharedPreferences

如果您不需要该属性,请将其删除并避免麻烦。

@Ahmed Hegazy 是正确的。问题是我在清单中添加了 android:process 以终止 intent service 因为我无法访问。