Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to get MainActivity for an Intent created in another class in Xamarin.Droid project?

$
0
0

I want to set alarm for Xamarin.Droid project. Use DependencyService for platform-specific functions, define an interface with the signature of these functions

namespace ExampleProgram.Droid
{
    public interface IRunAlarm
    {
        Intent setIntent();
        ...
    }
}

and class with implementation.

[assembly: Dependency(typeof(ExampleProgram.Droid.RunAlarm))]
    namespace ExampleProgram.Droid
    {
        public class RunAlarm: MainActivity, IRunAlarm
        {
            public Intent setIntent()
            {
                Intent i = new Intent(this, typeof(AlarmReceiver));
                return i;
            }
            ...
        }
    }

But, when I run the program, an exception occurs:

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

An exception occurred on this line:

Intent i = new Intent(this, typeof(AlarmReceiver));

How to handle this exception and how to get MainActivity correctly in this case?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>