Wednesday, 15 August 2012

java - Cannot cast View to Switch -



java - Cannot cast View to Switch -

i need simple thing done, maintain getting errors. have multiple switches each distinct id. everytime utilize (switch)findviewbyid(), gives me next error: cannot cast view switch. s defined switch. did research , have tried clean project , delete r file, still doesn't work.

protected void oncreate(bundle savedinstancestate) { ... switches = weekprogram.getswitches(day); //returns arraylist switches (int j = 0; j < switches.size(); j++) { switch (j) { case 5: b = (button) findviewbyid(r.id.timebone); s = (switch) findviewbyid(r.id.switchone); break; case 6: b = (button) findviewbyid(r.id.timebtwo); s = (switch) findviewbyid(r.id.switchtwo); break; case 7: b = (button) findviewbyid(r.id.timebthree); s = (switch) findviewbyid(r.id.switchthree); break; case 8: b = (button) findviewbyid(r.id.timebfour); s = (switch) findviewbyid(r.id.switchfour); break; case 9: b = (button) findviewbyid(r.id.timebfive); s = (switch) findviewbyid(r.id.switchfive); break; case 0: b = (button) findviewbyid(r.id.timebsix); s = (switch) findviewbyid(r.id.switchsix); break; case 1: b = (button) findviewbyid(r.id.timebseven); s = (switch) findviewbyid(r.id.switchseven); break; case 2: b = (button) findviewbyid(r.id.timebeight); s = (switch) findviewbyid(r.id.switcheight); break; case 3: b = (button) findviewbyid(r.id.timebnine); s = (switch) findviewbyid(r.id.switchnine); break; case 4: b = (button) findviewbyid(r.id.timebten); s = (switch) findviewbyid(r.id.switchten); break; } ... }

xml file:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/backgroundforapp" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.hti_thermostat.selectedday$placeholderfragment" > <button android:id="@+id/timebnine" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebten" android:layout_alignleft="@+id/timebten" android:text="09:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebeight" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebnine" android:layout_alignleft="@+id/timebfive" android:text="08:30 am" android:textsize="13sp" android:onclick="settimebutton"/> <button android:id="@+id/timebseven" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebeight" android:layout_alignleft="@+id/timebeight" android:text="07:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebsix" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebseven" android:layout_alignleft="@+id/timebseven" android:text="06:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebten" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_alignleft="@+id/timebeight" android:layout_alignparentbottom="true" android:layout_marginbottom="26dp" android:text="10:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebfive" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_alignleft="@+id/timebfour" android:layout_centervertical="true" android:text="05:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebfour" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebfive" android:layout_alignleft="@+id/timebthree" android:text="04:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebthree" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebfour" android:layout_alignleft="@+id/dayofweek" android:text="03:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebtwo" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebthree" android:layout_alignleft="@+id/timebthree" android:text="02:30 am" android:textsize="13sp" android:onclick="settimebutton" /> <button android:id="@+id/timebone" style="?android:attr/buttonstylesmall" android:layout_width="100dp" android:layout_height="30dp" android:layout_above="@+id/timebtwo" android:layout_alignleft="@+id/dayofweek" android:text="01:30 am" android:textsize="13sp" android:onclick="settimebutton"/> <textview android:id="@+id/dayofweek" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/timebone" android:layout_centerhorizontal="true" android:layout_marginbottom="15dp" android:text="monday" android:textcolor="#ecdfd5" android:textsize="35sp" /> <imageview android:id="@+id/sunsd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_aligntop="@+id/timebone" android:src="@drawable/sunshine" /> <imageview android:id="@+id/moonsd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/sunsd" android:layout_aligntop="@+id/timebsix" android:src="@drawable/moon" /> <switch android:id="@+id/switchone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_aligntop="@+id/timebone" /> <switch android:id="@+id/switchtwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/switchone" android:layout_aligntop="@+id/timebtwo" /> <switch android:id="@+id/switchthree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/timebthree" android:layout_alignleft="@+id/switchtwo" /> <switch android:id="@+id/switchfour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/switchthree" android:layout_below="@+id/timebthree" /> <switch android:id="@+id/switchsix" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/switchfive" android:layout_aligntop="@+id/timebsix" /> <switch android:id="@+id/switchseven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/switchsix" android:layout_below="@+id/timebsix" /> <switch android:id="@+id/switcheight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/timebeight" android:layout_alignleft="@+id/switchseven" /> <switch android:id="@+id/switchnine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/timebnine" android:layout_alignleft="@+id/switcheight" /> <switch android:id="@+id/switchten" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/timebten" android:layout_alignleft="@+id/switchnine" /> <switch android:id="@+id/switchfive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/timebfive" android:layout_alignleft="@+id/switchfour" />

you have switch class in project , code using instead of android.widget.switch.

if other switch in package, alter imports import android.widget.switch.

if in same package, can explicitly refer platform class replacing switch android.widget.switch in code.

java android casting switch-statement

No comments:

Post a Comment