I'm trying to use an external library from android into my Xamarin.Android application, but am facing quite a few issues.
The external library I require is a Calendar library. After looking at Xamarin Component's limited collection of Calendar library, I was unable to find one that suits the needs of my application. I then read up on using Android Studio's external libraries, using this Xamarin tutorial and this youtube tutorial.
However, of the two libraries I've tried -
Material Calendar View
Caldroid
both were not able to build.
Here is the error list when I try to build Material Calendar View:
The type or namespace name 'CalendarPagerView' does not exist in the namespace 'Com.Prolificinteractive.Materialcalendarview' (are you missing an assembly reference?)
'WeekView.ThresholdClass': no suitable method found to override
'WeekView.ThresholdType': no suitable method found to override
'WeekView.Rows': no suitable method found to override
'WeekView.BuildDayViews(ICollection<DayView>, Calendar)': no suitable method found to override
The type or namespace name 'DayView' does not exist in the namespace 'Com.Prolificinteractive.Materialcalendarview' (are you missing an assembly reference?)
'WeekView.IsDayEnabled(CalendarDay)': no suitable method found to override
'WeekView.OnClick(View)': no suitable method found to override
'WeekView.OnInitializeAccessibilityEvent(AccessibilityEvent)': no suitable method found to override
'WeekView.OnInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)': no suitable method found to override
'WeekView.SetDateTextAppearance(int)': no suitable method found to override
'WeekView.SetDayFormatter(IDayFormatter)': no suitable method found to override
'WeekView.SetMaximumDate(CalendarDay)': no suitable method found to override
'WeekView.SetMinimumDate(CalendarDay)': no suitable method found to override
'WeekView.SetSelectedDates(ICollection)': no suitable method found to override
'WeekView.SetSelectionColor(int)': no suitable method found to override
'WeekView.SetSelectionEnabled(bool)': no suitable method found to override
'WeekView.SetShowOtherDates(int)': no suitable method found to override
'WeekView.SetWeekDayFormatter(IWeekDayFormatter)': no suitable method found to override
'WeekView.SetWeekDayTextAppearance(int)': no suitable method found to override
'WeekView.ShouldDelayChildPressedState()': no suitable method found to override
'IExperimentalInvoker.Equals(Object)' hides inherited member 'Object.Equals(Object)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
'IExperimentalInvoker.GetHashCode()' hides inherited member 'Object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
'IExperimentalInvoker.ToString()' hides inherited member 'Object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
The member 'WeekView.java_class_handle' does not hide an inherited member. The new keyword is not required.
The member 'WeekView.class_ref' does not hide an inherited member. The new keyword is not required.
Error encountered while loading the project. Some project features, such as full solution analysis for the failed project and projects that depend on it, have been disabled.
top ancestor WeekPagerAdapter not found for nested type Com.Prolificinteractive.Materialcalendarview.WeekPagerAdapter.Weekly.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.IShowOtherDates.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.SavedState.1.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.4.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.ISelectionMode.
top ancestor MonthPagerAdapter not found for nested type Com.Prolificinteractive.Materialcalendarview.MonthPagerAdapter.Monthly.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.LayoutParams.
Unknown parameter type com.prolificinteractive.materialcalendarview.MaterialCalendarView in method OnRangeSelected in managed type Com.Prolificinteractive.Materialcalendarview.IOnRangeSelectedListener.
Invalid return type com.prolificinteractive.materialcalendarview.CalendarPagerView.LayoutParams in method GenerateLayoutParams in managed type Com.Prolificinteractive.Materialcalendarview.WeekView.
Invalidating Com.Prolificinteractive.Materialcalendarview.IOnRangeSelectedListener and all nested types because some of its methods were invalid.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.SavedState.
Unknown parameter type com.prolificinteractive.materialcalendarview.MaterialCalendarView in method OnDateSelected in managed type Com.Prolificinteractive.Materialcalendarview.IOnDateSelectedListener.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.1.
For StateBuilder, could not find enclosing type 'MaterialCalendarView'.
Unknown parameter type com.prolificinteractive.materialcalendarview.MaterialCalendarView in method OnMonthChanged in managed type Com.Prolificinteractive.Materialcalendarview.IOnMonthChangedListener.
Invalidating Com.Prolificinteractive.Materialcalendarview.IOnDateSelectedListener and all nested types because some of its methods were invalid.
Unknown parameter type com.prolificinteractive.materialcalendarview.MaterialCalendarView in method CalendarPagerView in managed type Com.Prolificinteractive.Materialcalendarview.CalendarPagerView.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.State.
Invalidating Com.Prolificinteractive.Materialcalendarview.IOnMonthChangedListener and all nested types because some of its methods were invalid.
top ancestor MaterialCalendarView not found for nested type Com.Prolificinteractive.Materialcalendarview.MaterialCalendarView.StateBuilder.
Unknown parameter type com.prolificinteractive.materialcalendarview.MaterialCalendarView in method WeekView in managed type Com.Prolificinteractive.Materialcalendarview.WeekView.
ignoring option UseSplitVerifier; support was removed in 8.0
missing class error was raised while reflecting com.prolificinteractive.materialcalendarview.MaterialCalendarView : android/support/v4/view/ViewPager$OnPageChangeListener
Couldn't load class com/prolificinteractive/materialcalendarview/MonthPagerAdapter : java.lang.NoClassDefFoundError: android/support/v4/view/PagerAdapter
Couldn't load class com/prolificinteractive/materialcalendarview/CalendarPager : java.lang.NoClassDefFoundError: android/support/v4/view/ViewPager
Couldn't load class com/prolificinteractive/materialcalendarview/MaterialCalendarView$3 : java.lang.NoClassDefFoundError: android/support/v4/view/ViewPager$PageTransformer
Couldn't load class com/prolificinteractive/materialcalendarview/CalendarPagerAdapter : java.lang.NoClassDefFoundError: android/support/v4/view/PagerAdapter
Couldn't load class com/prolificinteractive/materialcalendarview/MaterialCalendarView$2 : java.lang.NoClassDefFoundError: android/support/v4/view/ViewPager$OnPageChangeListener
Couldn't load class android/support/v4/view/BetterViewPager : java.lang.NoClassDefFoundError: android/support/v4/view/ViewPager
Couldn't load class com/prolificinteractive/materialcalendarview/WeekPagerAdapter : java.lang.NoClassDefFoundError: android/support/v4/view/PagerAdapter
Caldroid also produces a similar error list.
I have tried directly using existing Xamarin libraries for the library, such as the one from this question but also had similar build problems, which led me to believe I was probably doing something obviously wrong, but I can't figure it out.
As I'm rather new to Android development, a step-by-step guide to bind this exact library would help me out a lot.
---Update---
After building the library and adding it as a reference in my project, viewing the library in the object browser shows that it is empty.
Missing members and error:
In your project solution, you could choose Add Existing Project, add a MaterialCalendarLibrary from Xamarin.MaterialCalendarView project.
Then you could follow this Xamarin tutorial, to use the material-calendarview. All works fine and if I found a better solution I will come back and update this answer.
Effect like this.
EDIT :
Its object browser.
Related
I try to create a POST request with Android Volley but when I type volleyRequestQueue = Volley.newRequestQueue(this), this is a error "this is not defined in this context".
I know that I have this problem because my function can't resolve Android Context but I don't understand how to import the context in my file.
Here you have my project scheme:
and the error
The function is called in the "FormsAddAliments" file.
Thanks you !
The error "this is not defined in this context" is not referring to the abstract class android.content.Context, but rather the context of the code.
You are using this in a top level function in a file. There is no object that this can be a reference to, if you want it to be a top level function and have an object that this can reference, you will need to declare it as an extension function on the type that you want this to reference like Context.sendFoodToServer(...). This should work, but it is likely violating some design principles to do it this way (usually passing a context around is a bad idea because it can lead to context leaking).
I have started working with a Kotlin Multiplatform library, and I make use of Gson on the android side to do some processes. Since I know iOS does not have support for Gson, I decided to create an expected function called convertToJson which has a return type of a class. However when I have the actual implementation, I get the following error:
None of the arguments can be called with the arguments supplied
This occurs at this line of code:
actual fun convertFromJson(json:String): ClassName {
return Gson().fromJson(json, ClassName::class) // this is where the error lies
}
I also have an actual implementation of a function called convertToJson which does not give me an error, so I am wondering why the above mentioned convertFromJson function gives me an error.
Any help or advice will be highly appreciated.
Gson is implemented in java and its fromJson accepts an instance of java.lang.Class as the argument, so you should replace ClassName::class with ClassName::class.java, because the former gives you an instance of kotlin.reflect.KClass.
My ViewModel class implements LifecycleObserver.
When I call fragment.lifecycle.addObserver(this) it produces exception.
Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.
Strange, that firstly it was working fine, but not long ago this exception has appeared. I've found, that audioFocusRequest is cause of this bug.
private val audioFocusRequest by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(this)
.build() else throw RuntimeException("Can't be done for Android API lower than 26")
}
Does anybody know how it can be fixed?
UPD
Tried to use annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version", but got compilation error:
(decided to paste screenshot, because whole logs are quite big)
UPD 2
At the end I've decided to delete audioFocusRequest field and to use old deprecated method - requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) instead of recommended requestAudioFocus(#NonNull AudioFocusRequest focusRequest)
It helped me to make code working again, so it can be solution. But I didn't find answer - why this problem had appeared. It strange because code used to be working before.
So problem has been solved but question still stays unanswered
Try to use kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
The class which implements LifecycleObserver has some method, which has parameters with type that only exist for higher APIs.
Your variables (i guess) and function parameters must exist on all APIs even function is not called (maybe this is requirement for classes who implement LifecycleObserver).
A possible solution is to change function parameter type to Any (kotlin) or Object (Java) and cast it to appropriate type inside function.
I have to remove this set method on SpinnerView: lifecycleOwner = viewLifecycleOwner
I was able to fix this by moving the offending methods into another class, but still called from my LifecycleObserver. After reading the error message again:
Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.
It seems as though no methods or objects are allowed in the class extending LifecycleObserver if they don't exist in the device's OS, even if they are wrapped in an SDK version check and never accessed.
I have implemented a Google Map in my Xamarin forms app but am getting the above exception when navigating away from the page before the map has completed loading the current location.
This is probably the same issue previously raised but unanswered here.
From my research I believe the issue is related to leaky abstration answer given in this separate question: MonoDroid: Error when calling constructor of custom view - TwoDScrollView
However I do not have enough knowledge of Java or Android development to know how to resolve this issue. I am hoping that someone can explain where and how I can handle this exception when it occurs. Basically what I believe I need to achieve is catching the exception and handle it in the Droid project, but where?
These are the key exception messages that I am getting.
Message: [NotSupportedException: Unable to activate instance of type Xamarin.Forms.Maps.Android.MapRenderer from native handle 0xbef7ad5c (key_handle 0xd4608e7).]
Message: [MissingMethodException: No constructor found for Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)]
Message: [Exception of type 'Java.Interop.JavaLocationException' was thrown.]
However I do not have enough knowledge of Java or Android development to know how to resolve this issue. I am hoping that someone can explain where and how I can handle this exception when it occurs.
I think the reason is when you are navigating the pages, the Dispose method of Xamarin.Forms.Maps.Android.MapRenderer is called before the loading of the rest of the map. ACW need to create a new instance of MapRenderer but failed to create a new one because MapRenderer has no constructor method of (IntPtr, JniHandleOwnership).
If you refer to Premature Dispose() Calls you can find following statement:
If the subclass does contain an (IntPtr, JniHandleOwnership) constructor, then a new instance of the type will be created.
So the workaround for this exception is to create a subclass(Let's say MyMapRenderer) for Xamarin.Forms.Maps.Android.MapRenderer,which has a constructor with two arguments: (IntPtr, JniHandleOwnership), and use MyMapRenderer for map rendering:
In PCL create a custom control for Xamarin.Forms.Map.Map and use MyMap instead in your project:
public class MyMap:Map{}
Create a Custom MapRenderer in Droid project,which has a (IntPtr, JniHandleOwnership) contructor:
[assembly:ExportRenderer(typeof(MyMap),typeof(MyMapRenderer))]
namespace YourNameSpace.Droid
{
public class MyMapRenderer:MapRenderer
{
public MyMapRenderer(IntPtr handle, JniHandleOwnership transfer) { }
}
}
For details about create a custom MapRenderer, please refer to Custom a Map.
Updated version for answer from Elvis. Constuctor should look like as below.
public MyMapRenderer(System.IntPtr handle, JniHandleOwnership transfer): base(Android.App.Application.Context) { }
And additionally most probably you will also an empty constuctor like below. Otherwise first instance creation fails. Also for Proguard it is important.
public MyMapRenderer(Context context) : base(context)
{
}
i'm trying to bind the core library: https://github.com/gabrielemariotti/cardslib
I downloaded the .AAR file from Maven Central and did everything in this tutorial Binding an .AAR, but when i'm building the Binding Library i'm getting this errors:
'CardExpandableListAdapter' does not implement inherited abstract member 'BaseExpandableListAdapter.GetGroup(int)'
Inconsistent accessibility: return type 'CardWithList.LinearListAdapter' is less accessible than method 'CardWithList.GetLinearListAdapter()'
Inconsistent accessibility: parameter type 'CardWithList.LinearListAdapter' is less accessible than method 'CardWithList.SetLinearListAdapter(CardWithList.LinearListAdapter)'
Inconsistent accessibility: property type 'CardWithList.LinearListAdapter' is less accessible than property 'LinearListView.Adapter'
'CardView' does not implement interface member 'ICardViewWrapper.SetExpanded(bool)'
'CardView' does not implement interface member 'ICardViewWrapper.SetForceReplaceInnerLayout(bool)'
'CardView' does not implement interface member 'ICardViewWrapper.SetLongClickable(bool)'
'CardView' does not implement interface member 'ICardViewWrapper.SetOnExpandListAnimatorListener(ICardViewWrapperOnExpandListAnimatorListener)'
'CardView' does not implement interface member 'ICardViewWrapper.SetRecycle(bool)'
It looks like the binding generator has some bugs, is there any fix for this?
Thanks in advance.
From Xamarin's page,
When you are generating Java Bindings, there are several common error scenarios that you may run into.
Here is the link to possible errors you might run into and how to resolve them.
Link for how to fix "Interface does not implement method"
Inconsistent accessibility issue can be resolved by changing the visibility accordingly. Here is an example of how to modify visibility
<attr path="/api/package[#name='com.somepackage']/class[#name='SomeClass']" name="visibility">public</attr>