Reputation: 2163
I got a weird exception comming up it worked for 1 year on android when i do:
TimerButton btnOut = FindViewById<TimerButton>(Resource.Id.btnTimer);
it throws me an exception like:
System.InvalidCastException: Unable to convert instance of type 'Android.Widget.Button' to type 'MyProject.TimerButton'
The weird part is TimerButton
inherits from Android.Widget.Button ?
like:
public class TimerButton : Android.Widget.Button
{
public int MaxTicks { get; set; }
public EventHandler AfterTimer;
private String OldButtonText { get; set; }
public ButtonState State
{
get { return this._state; }
}
protected void Initialize()
{
if (MaxTicks == 0)
{ MaxTicks = 10; }
OldButtonText = this.Text;
this.Click += (sender, e) =>
{
if (this._state == GSDButtonState.Normal)
{
this.SetState(GSDButtonState.Ticking);
}
else
{
this.SetState(GSDButtonState.Normal);
}
};
}
public TimerButton(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer ) : base (javaReference, transfer)
{
this.Initialize ();
}
public TimerButton(Context context, Android.Util.IAttributeSet attrs) : base (context, attrs)
{
this.Initialize ();
}
public TimerButton (Context context, Android.Util.IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle)
{
this.Initialize ();
}
}
Its just a short example since im using this button with a custom eventhandler so i can do something in about x seconds. not important. So i stripped some code out such as SetState
Note this worked for like 1 year orso
What i tried:
if i use a simple button it won't crash. Resource.Id.btnTimer ofcourse exists in the XML layout as Button
pls does someone has any ideas or tips?
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:minWidth="25px"
android:minHeight="25px">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Status"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:id="@+id/txtStatus" />
<Button
android:text="Timer Button"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/ButtonState"
android:shadowColor="#C4BEBE"
android:layout_gravity="center"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginTop="10.1dp"
android:layout_marginBottom="10.1dp"
android:id="@+id/btnTimer" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Upvotes: 0
Views: 2548
Reputation: 74209
Change your Button
in your layout to {your package name here}.TimerButton
Something like:
com.sushihangover.custombutton.XAMLButton
Example XAML:
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.sushihangover.custombutton.XAMLButton
android:id="@+id/customButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
You must register your C#/Java wrappers so they have a fully qualified Java class name:
[Register("com.sushihangover.custombutton.XAMLButton")]
public class XAMLButton : Button
Upvotes: 1
Reputation: 3455
Your btnTimer
is a Button
not TimerButton
. Replace <Button>
with <MyProject.TimerButton>
. Replace MyProject
with the NameSpace of the TimerButton
class.
Upvotes: 1