What is causing InvalidCastException?

BenAshbyBenAshby USMember ✭✭

I had an application that appeared to be working great. However, when I add an EditText (or any other view) to .axml and Activity I receive an InvalidCastException error stating that it cannot convert type ...EditText to ...LinearLayout (or whatever the next view in line happens to be). Can anybody help explain what is going on? It seems like something is off in the Resource.Designer.cs file, but I don't know enough to identify. I cannot figure out what would've changed to no longer allow me to add any more controls to this particular axml/Activity (I am able to add controls to other layouts without issue). I have tried rebuilding and cleaning the solution, but that did not work.

Answers

  • BenAshbyBenAshby USMember ✭✭

    I am able to add a new view as the last view in the .axml without issue, but if I try to move it ahead of others the InvalidCastException occurs.

  • BKurtanidzeBKurtanidze Member

    there is relatively simple but a bit annoying answer to your question. You cant cast EditText to LinearLayout. If you will print yourLinearLayot.GetType() in the log result will be something like this object.(SomeOtherTypesWhichIDontRemember).ViewGroup.LinearLayout you can cast your layout to ViewGroup or Object but you cant cast it to EditText.

  • BenAshbyBenAshby USMember ✭✭

    I am not trying to cast the EditText to LinearLayout. I am receiving this error by simply adding an EditText to the .axml (don't even have to refer to it in Activity). Up until a couple days ago, I was able to add controls just fine to this file, but all of the sudden I cannot add or (as recently discovered) remove views from this file without receiving a similar error, and I am not sure what changed to break it.

  • BKurtanidzeBKurtanidze Member

    Pardon me for little misunderstanding have you tried to use same code in empty project?

  • BenAshbyBenAshby USMember ✭✭

    I tried to create a new .axml and Activity within the project, but that broke something else within one of the other Activities. I will try this suggestion.

  • BenAshbyBenAshby USMember ✭✭

    I tried copying over to .axml and Activity code to blank project, but the Resource.Designer.cs file is not adding the ids so when I try to reference the views in Activity I do not have access to Resource.Id... I started seeing an error telling me to look at ActivityLog.xml which is showing the following error, so I am going to follow that path now.

    System.NullReferenceException: Object reference not set to an instance of an object. at Xamarin.VisualStudio.Android.Designer.MonoAndroidDesignerService.FindBestAndroidTarget(DesignerProject designerProject, Int32 apiLevel) in E:\A_work\12\s\Xamarin.Designer.VisualStudio\src\VisualStudio.Android.Designer\MonoAndroidDesignerService.cs:line 136 at Xamarin.VisualStudio.Android.Designer.AxmlTextViewCreationListener.VsTextViewCreated(IVsTextView textViewAdapter) in E:\A_work\12\s\Xamarin.Designer.VisualStudio\src\VisualStudio.Android.Designer\Editor\AxmlTextViewCreationListener.cs:line 59 at Microsoft.VisualStudio.Editor.Implementation.SimpleTextViewWindow.<>c__DisplayClass565_0.b__0() at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.CallExtensionPoint(Object errorSource, Action call) --- End of stack trace from previous location where exception was thrown --- at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

Sign In or Register to comment.