M
M
Michaellux2018-08-02 15:46:58
Android
Michaellux, 2018-08-02 15:46:58

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

I want to set an alarm for a Xamarin.Droid project. To do this, I use DependencyService to define platform-specific functions. I define an interface with a function signature

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

and a class with an 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;
            }
            ...
        }
    }

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

on this line:
Intent i = new Intent(this, typeof(AlarmReceiver));

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="3" android:versionName="3" package="com.example.exampleprogram">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <application android:label="exampleprogram" android:icon="@drawable/appicon"></application>
</manifest>

How to handle this exception, and how can MainActivity be correctly retrieved in this case?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question