1

私のアプリケーションでは、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 を引き起こすために実際に何が起こったのでしょうか?? 助けてください。

4

2 に答える 2

0

ここでContext使用する必要があるthisのは、アクティビティ コンテキストです。

ご了承ください:

Application Cotext : Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

于 2013-08-30T10:31:22.187 に答える