私のアプリケーションでは、A は最初に起動されるアクティビティです。コードは次のとおりです。
public class A extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here I'm checking if the user has already got registered, using shared preference. If yes, I want to fetch the version of this application and start Activity B. Other wise perform some other operation.
if(Registered) {
//Here, I'm trying to fetch the version of my own application.
PackageManager manager = getApplicationContext()
.getPackageManager();
PackageInfo info;
try {
info = manager.getPackageInfo(getApplicationContext()
.getPackageName(), 0);
String version = info.versionName;
} catch(NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
したがって、アプリケーションがインストールされるとすぐに、このコードが実行されます。ここで NameNotFoundException を取得しています。キャッチされていますが、まだ問題が発生しています。
上記のコードで getApplicationContext を null にすることはできません。そうである場合、「manager」は null になり、null ポインター例外が発生します。NameNotfoundException を引き起こすために実際に何が起こったのでしょうか?? 助けてください。