我都有Android語境有點問題,我不知道如何解決problem.Here是我使用的代碼:機器人方面的NullPointerException
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
。
有什麼建議嗎?
你有想過嗎?剛剛得到同樣的行爲。 (答案令人驚訝地沒用) –