我在使用android Context时遇到了一些问题,不知道该怎么解决。这是我正在使用的代码:
public class TestActivity {
Context context;
public static String getPackageVersion(){
try {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
version = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
public static boolean checkClientApiVer(String clientApiVer){
int s = RPCCommunicator.strVerToIntVer(clientApiVer);
int c = RPCCommunicator.strVerToIntVer(getPackageVersion());
return (c>=s);
}
public boolean execute() {
serverApiVer = jsonObj.getString("server_api_ver");
Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);
checkClientApiVer(serverApiVer);
}
它显示在这一行中出现了空指针异常:
PackageManager pm = context.getPackageManager();
实际上我不能使用this.getPackageManager()
或者TestActivity.getPackageManager()
,同时我也不能将context
设置为this
。
有什么建议吗?