I created a BroadcastReceiver which is constructed from a Service. The BroadcastReceiver needs to interact with the Service, so I pass this to the constructor. After adding the [BroadcastReceiver]-Attribute and trying to compile (with MonoDevelop), I get
D:\Programme\MSBuild\Novell\Novell.MonoDroid.Common.targets(3,3): Error MSB4018: Unerwarteter Fehler bei der GenerateJavaStubs-Aufgabe.
System.InvalidOperationException: The type 'keepass2android.TimeoutService/MyBroadcastReceiver' needs to have a public default constructor.
at Xamarin.Android.Tasks.ManifestDocument.Merge(List`1 subclasses, List`1 selectedWhitelistAssemblies, Boolean embed)
at Xamarin.Android.Tasks.GenerateJavaStubs.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult) (MSB4018) (keepass2android)
Why does Mono for Android need this default constructor? Will it ever create my BroadcastReceiver with the default constructor (in this case, the BroadcastReceiver wouldn’n work correctly)?
It’s is only required if you are registering
BroadcastReceiverusingAndroidManifest. Android uses reflection to instantiate the receiver and it needs a default constructor (otherwise, it doesn’t know which arguments should be passed).It looks like you are registering your broadcast receiver in code using
Context.registerReceiver. In this case you don’t need to addreceivertag in theAndroidManifest