John Raesly
John Raesly

Reputation: 308

An object reference is required for the non-static field, method, or property 'Android.Content.Intent.GetStringExtra(string)'

public override void OnCreate()
{
    base.OnCreate();

    //Find our audio and notificaton managers
    audioManager = (AudioManager)GetSystemService(AudioService);
    wifiManager = (WifiManager)GetSystemService(WifiService);

    string text = Intent.GetStringExtra ("MyData") ?? "Data not available";

Error CS0120: An object reference is required for the non-static field, method, or property 'Android.Content.Intent.GetStringExtra(string)' (CS0120)

I keep getting this error for the last line of the code. I am trying to pass data from a qr code to the next activity the same way they do it in here

I am not sure how to fix this problem and I have tried creating it static which didn't fix it.

Upvotes: 0

Views: 3524

Answers (3)

Sweeper
Sweeper

Reputation: 271135

getStringExtra is a non-static method. This means that you need an object to call it. For getStringExtra, you need an Intent object to call it, not the class.

So how do you get an intent object? In an activity, you can use getIntent to get the intent that is passed to the activity. Now you have the object, you can call getStringExtra on the object returned:

var obj = getIntent();
var someString = obj.getStringExtra(someArgs);

You can simplify this to

var someString = getIntent().getStringExtra(someArgs);

Upvotes: 1

ARK
ARK

Reputation: 46

Pass the data from Activity1 to Activity2 as below:

Activity1 Passing Data

Intent intent = new Intent(Activity1.this, Activity2.class);

intent.putExtra("key","value");

startActivity(intent);

Activity2 Getting Data, Write below code in your Activity2 onCreate method

String data = getIntent().getStringExtra("key");

Upvotes: 0

OrcusZ
OrcusZ

Reputation: 3660

GetStringExtra is used to pass data between two activities. You put only one of that activity... Did you use this feature correctly ?

More information here : https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

Upvotes: 0

Related Questions