I am using MonoDevelop for Android with the MapsAndlocationDemo and I have a question about starting another activity.

Here is my code:

    public void loadActivity (Context ActivityContext)
    {
        var second = new Intent(ActivityContext, typeof(LocationDetailsActivity));
        second.PutExtra("FirstData", "Data from FirstActivity");
        StartActivity (second);
    }

This code works perfectly when called from the MapWithOverlayActivity : MapActivity class. I am wanting to call it from the MapItemizedOverlay: ItemizedOverlay class when the OnTap method of a map marker is called.

I have tried to make the method static. Here is my code for this:

    static public void loadActivity (Context ActivityContext)
    {
        var second = new Intent(ActivityContext, typeof(LocationDetailsActivity));
        second.PutExtra("FirstData", "Data from FirstActivity");
        StartActivity (second);
    }

However, I now get the following error: An object reference is required for the non-static field, method or property Android.Content.Context.StartActivity(Android.Content.Intent)

Can I please have some information as to why this does not work, and some help to get it working.

有帮助吗?

解决方案

You can use Android's global Application object if it helps. Here's an example:

public class MyApp extends Application{
    private String foo = null;

    public String getFoo(){
        return foo;
    }
}

public class MyActivity extends Activity {
    private MyApp app;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        app = ((MyApp)getApplication());

        String thing = app.getFoo();
    }
}

If you need the method to be accessible from anywhere you can replace the getFoo method with your Intent :) I think it should work. Application reference is here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top