My Toggle button crashes when changing it's state. My situation: If I change the state of toggle button from Off to On which Suddenly, crashes the whole app and says "YourApp is not Responding". I don't know what is the error in the code. I am a newbie in Android App Developing. So, I don't know how to find and rectify the errors. If anybody know plz help me in my case. Thanks in Advance!
This is my XML file:
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="-2dp"
android:layout_marginLeft="-45dp"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn="" />
This is my Java code:
/*------------------------------Screen timeout toggle----------------------------------------------------*/
ToggleButton toggle_button = (ToggleButton) findViewById(R.id.toggle_button);
toggle_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
timeout_on = Toast.makeText(getBaseContext(), "Screen Timeout is Disabled (Your screen will doesn't sleep from now!)", Toast.LENGTH_LONG);
TextView v = (TextView) timeout_on.getView().findViewById(android.R.id.message);
if( v != null) v.setGravity(Gravity.CENTER);
timeout_on.show();
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
timeout_off = Toast.makeText(getBaseContext(), "Screen Timeout is Enabled (Your screen will go to sleep as your phone's timeout!)", Toast.LENGTH_LONG);
TextView v = (TextView) timeout_off.getView().findViewById(android.R.id.message);
if( v != null) v.setGravity(Gravity.CENTER);
timeout_off.show();
}
} catch (Exception error) {
error.printStackTrace();
}
}
});
/*------------------------------Screen timeout toggle----------------------------------------------------*/
My answer to your question--How to find why? is to see the error stack trace in the Logcat window:
You may find some clues from it.