I have a Xamarin Android project. I am trying to use a custom Action Provider. I am vaguely following the Android java example here: https://gist.github.com/f2prateek/3982054 but porting it to C#. Therein lies the problem.
I created the class SearchActionProvider with the following code:
namespace Erik.Mobile.Screens
{
public class SearchActionProvider : ActionProvider
{
...
}
}
The menu resource I've created:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/actionSearch"
android:icon="@drawable/searchIcon"
android:title="@string/actionSearch"
android:showAsAction="ifRoom"
android:actionProviderClass="Erik.Mobile.Screens.SearchActionProvider"
/>
</menu>
My package name is com.company.Erik.
In my Activity, I overridden OnCreateOptionsMenu() to use MenuInflater.Inflate() on the resource. I assume it is working, because the icon for the ActionProvider is showing up in the activity bar. Clicking it does nothing.
In Xamarin studio, when I start, I get the following error, edited for brevity:
[MenuInflater] Cannot instantiate class: Erik.Mobile.Screens.SearchActionProvider
[MenuInflater] java.lang.ClassNotFoundException: Didn't find class "Erik.Mobile.Screens.SearchActionProvider" on path: /data/app/com.company.Erik-1.apk
[MenuInflater] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
[MenuInflater] at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
[MenuInflater] at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
[MenuInflater] at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:469)
[MenuInflater] at android.view.MenuInflater$MenuState.readItem(MenuInflater.java:374)
[MenuInflater] at android.view.MenuInflater.parseMenu(MenuInflater.java:160)
[MenuInflater] at android.view.MenuInflater.inflate(MenuInflater.java:110)
...
So, clearly I have the wrong value in my android:actionProviderClass in my menu resource. But I have tried many alternates, and nothing seems to work. I tried "com.company.Erik.SearchActionProvider” and “com.company.Erik.Erik.Mobile.Screens.SearchActionProvider".
How is one supposed to refer to a class within an XML file in Xamarin?
I received information back from Xamarin. It seems that case is important, but not in the way I expected.
My namespace is Erik.Mobile.Screens, which has capitalized the first letter of each work.
In the android:actionProviderClass, I need to refer to the namespace in all lowercase. So my class name needs to be erik.mobile.screens.SearchActionProvider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With