简体   繁体   English

非静态字段,方法或属性'Android.Content.Intent.GetStringExtra(string)'需要对象引用

[英]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) 错误CS0120:非静态字段,方法或属性'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. 我不确定如何解决此问题,并且我尝试将其创建为静态但无法解决。

getStringExtra is a non-static method. getStringExtra是一个非静态方法。 This means that you need an object to call it. 这意味着您需要一个对象来调用它。 For getStringExtra , you need an Intent object to call it, not the class. 对于getStringExtra ,您需要一个Intent对象来调用它,而不是类。

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. 在活动中,可以使用getIntent获取传递给活动的意图。 Now you have the object, you can call getStringExtra on the object returned: 现在有了对象,可以对返回的对象调用getStringExtra

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

You can simplify this to 您可以简化为

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

GetStringExtra is used to pass data between two activities. GetStringExtra用于在两个活动之间传递数据。 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/ 此处的更多信息: https : //developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

Pass the data from Activity1 to Activity2 as below: 将数据从Activity1传递到Activity2,如下所示:

Activity1 Passing Data Activity1传递数据

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 Activity2获取数据,在Activity2 onCreate方法中编写以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性是否需要对象引用? - An object reference is required for the non-static field, method, or property? 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性(数据集)需要对象引用 - An object reference is required for the non-static field, method, or property (dataset) 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method or property 非静态字段、方法或属性需要 object 引用 - An object reference is required for the non-static field, method, or property GetAbbreviatedMonthName中的非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property in GetAbbreviatedMonthName
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM