Bug 9630 - bundle.GetString throws exception
Summary: bundle.GetString throws exception
Alias: None
Product: Android
Classification: Xamarin
Component: Debugger ()
Version: 4.8.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-01-16 18:13 UTC by Paul Keefe
Modified: 2013-01-16 21:19 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Paul Keefe 2013-01-16 18:13:56 UTC
When I call bundle.GetString("string_name") I get an unhandled exception of: "Unknown member: GetString".

Here is a simple use case:

The string is stored in the manifest as:

<application android:label="MyApplication" android:icon="@drawable/icon">
        <meta-data android:name="com.myapplication.API_KEY" android:value="XXX" />

and the code in my start up activity is:

protected override void OnCreate (Bundle bundle)
    base.OnCreate (bundle);

    string APIKey = bundle.GetString("com.myapplication.API_KEY");  // <<== Exception thrown.
Comment 1 Jonathan Pryor 2013-01-16 21:19:38 UTC
I believe that you're confused, or you need to provide a larger example.

Firstly, I'm unable to get a unhandled exception of "Unknown member: GetString". Instead, I get:

> UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
>   at Scratch.Bxc9630.Activity1.OnCreate (Android.OS.Bundle)
>   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr)
>   at (wrapper dynamic-method) object.bfe09a25-d257-4450-9d42-4f4d9904f78c (intptr,intptr,intptr)

The reason for the NullReferenceException is that the `bundle` parameter is null:

> If the activity is being re-initialized after previously being shut
> down then this Bundle contains the data it most recently supplied in
> Activity.OnSaveInstanceState(Android.OS.Bundle).
> Note: Otherwise it is null.

Furthermore, your <meta-data/> element is meaningless; 

> contained in: <activity> <activity-alias>  <service> <receiver>

The paraent element of your <meta-data/> element is <application/>, which isn't listed as a valid parent.

Finally, it appears that you're trying to use `bundle.GetString()` in order to obtain the value for the metadata key "com.myapplication.API_KEY".

However, I believe I know why you're confused; the above documentation states:

> android:value
> Type: String value...  Bundle method: getString()

which does imply that Bundle.getString() is the method to use. However, the documentation also states:

> A component element can contain any number of <meta-data>
> subelements. The values from all of them are collected in a single
> Bundle object and made available to the component as the 
> PackageItemInfo.metaData field.

Translating this to C#, along with translating the <meta-data/> element into a custom attribute, and we get this:

  [Activity (Label = "Scratch.Bxc9630", MainLauncher = true)]
  [MetaData ("metadata.name", Value = "metadata.value")]
  public class Activity1 : Activity
    int count = 1;

    protected override void OnCreate (Bundle bundle)
      base.OnCreate (bundle);

      PackageInfo   packageInfo   = PackageManager.GetPackageInfo (
          PackageName, PackageInfoFlags.Activities | PackageInfoFlags.MetaData);
      ActivityInfo  activityInfo  = packageInfo.Activities [0];
      string        value         = activityInfo.MetaData.GetString (

      // Set our view from the "main" layout resource
      SetContentView (Resource.Layout.Main);

      // Get our button from the layout resource,
      // and attach an event to it
      Button button = FindViewById<Button> (Resource.Id.myButton);
      button.Text = value;

      button.Click += delegate {
        button.Text = string.Format ("{0} clicks!", count++);

We use PackageManager to get information about the currently executing package, which allows us to get information about a particular activity, from which we can obtain access to the Activity's metadata.