我正在尝试从清单文件访问活动的元数据。
清单如下所示:
<activity
android:name="co.uk.benbun.nvrrclubapp.MainActivity"
android:label="@string/app_name" >
<meta-data android:value="newstag" android:name="NEWS"></meta-data>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
访问它的代码如下所示:
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("NEWS");
} catch (NameNotFoundException e) {
Log.e("metadata", "Failed to load meta-data, NameNotFound: " + e.getMessage());
feedTag = "NEWS";
} catch (NullPointerException e) {
Log.e("metadata", "Failed to load meta-data, NullPointer: " + e.getMessage());
feedTag = "NEWS";
}
当我执行代码时,我总是得到 nullPointerException。
我究竟做错了什么?