我想为Xamarin.Droid项目设置警报。将DependencyService用于特定于平台的功能,使用这些功能的签名定义接口
namespace ExampleProgram.Droid
{
public interface IRunAlarm
{
Intent setIntent();
...
}
}
并带有实现类。
[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;
}
...
}
}
但是,当我运行程序时,会发生异常:
Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
此行发生异常:
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>
在这种情况下如何处理该异常以及如何正确获取MainActivity?