【发布时间】:2013-08-24 22:04:20
【问题描述】:
我有以下代码:
private static AppWidgetService mInstance = null;
public static void startRefresh() {
AppWidgetProvider.setRefreshingState(mInstance
.getApplicationContext());
AppWidgetManager.refreshHandler(mInstance.getApplicationContext());
}
运行时有时会失败,有时会通过。
对于明显的异常:
cannot call a non-static method from a static context
我很困惑mInstance 是静态的,
所以它的实例方法可以从静态上下文中调用。没有?
如果有时失败怎么办?
【问题讨论】:
-
肯定的。您可以调用 AppWidgetService 的静态或非静态方法,但方法 'setRefreshingState()' 和 'refreshHandler()' 是静态的。因此,要么从方法头中删除静态,要么使用这些类的实例调用这些方法。
-
我不明白。 startRefresh 是静态的,因此它可以调用同样是静态的 setRefreshingState()' 和 'refreshHandler()'。为什么建议
So either you remove static from your method header or call these methods using instances of these classes. -
您确定拨打
startRefresh有问题吗?那些应该没问题。但是,您的mInstance变量可能未初始化?
标签: java android eclipse static