Christopher Mckenzie Christopher Mckenzie - 1 year ago 38
Java Question

More than one toggle button causes app to crash

I have 2 toggle buttons. The first one "on_off_toggle_button" works fine. But once I press the second one the app crashes. The first button will work fine and do it's thing but the second one always crashes the app regardless if it's doing any or not. If I swap it so the second button has the onClick action I still get a crash. I think it's something to do with the toggle itself but I'm not sure what I'm doing wrong.

Anyone know why this is?

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/border_line"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/activity_horizontal_padding"
android:paddingRight="@dimen/activity_horizontal_padding"
android:weightSum="1">

<TextView
android:id="@+id/on_off_title"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_weight="1.0"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:paddingRight="@dimen/activity_vertical_padding"
android:text="Off/On"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold" />

<ToggleButton
android:id="@+id/on_off_toggle_button"
android:layout_width="100dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/toggle_selector"
android:checked="false"
android:onClick="startLockScreenService"
android:text=""
android:textOff=""
android:textOn="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/border_line"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/activity_horizontal_padding"
android:paddingRight="@dimen/activity_horizontal_padding"
android:weightSum="1">

<TextView

android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_weight="1.0"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:paddingRight="@dimen/activity_vertical_padding"
android:text="Off/On"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold" />

<ToggleButton
android:id="@+id/on_offnotif_button"
android:layout_width="100dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/toggle_selector2"
android:checked="false"
android:onClick=""
android:text=""
android:textOff=""
android:textOn="" />
</LinearLayout>

Answer Source

Issue : there is an empty android:onClick="" handler so when you click then OS will not be able to find any handler and an exception will be thrown

Solution

Either add a corresponding handler function like you did with other ToggleButton

OR remove android:onClick="" and then you can initialize your button and bind listener using anonymous class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download