Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9286265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:22:29+00:00 2026-06-18T19:22:29+00:00

For some reason my app crashes on launch.. heres all the code and debug

  • 0

For some reason my app crashes on launch.. heres all the code and debug output. It seems to me it’s something with how I am using setOnCheckedChangeListener..

Debug output:

02-12 05:12:03.048: E/AndroidRuntime(1464): FATAL EXCEPTION: main
02-12 05:12:03.048: E/AndroidRuntime(1464): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dugley.check.in/com.dugley.check.in.MainActivity}: java.lang.ClassCastException: com.dugley.check.in.MainActivity cannot be cast to android.widget.CompoundButton$OnCheckedChangeListener
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:137)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)
02-12 05:12:03.048: E/AndroidRuntime(1464): Caused by: java.lang.ClassCastException: com.dugley.check.in.MainActivity cannot be cast to android.widget.CompoundButton$OnCheckedChangeListener
02-12 05:12:03.048: E/AndroidRuntime(1464):     at com.dugley.check.in.MainActivity.onCreate(MainActivity.java:21)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.Activity.performCreate(Activity.java:5104)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 05:12:03.048: E/AndroidRuntime(1464):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Main_activity.xml

<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"
    tools:context=".MainActivity" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="30dp"
        android:text="Service"/>

</RelativeLayout>

MainActivity.java:

package com.dugley.check.in;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Switch s = (Switch) findViewById(R.id.switch1);

        if (s != null) {
            s.setOnCheckedChangeListener((OnCheckedChangeListener) this);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(this, "The Switch is " + (isChecked ? "on" : "off"),
                       Toast.LENGTH_SHORT).show();
        if(isChecked) {
            //do stuff when Switch is ON
        } else {
            //do stuff when Switch if OFF
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:22:30+00:00Added an answer on June 18, 2026 at 7:22 pm

    you need to add implements OnCheckedChangeListener with your MainActivity

    I made your code working, See below….

    public class MainActivity extends Activity implements OnCheckedChangeListener{//Changed
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Switch s = (Switch) findViewById(R.id.switch1);
    
        if (s != null) {
            s.setOnCheckedChangeListener(this); // Changed
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason whenever i click on my pin the app crashes. there are
For some reason, my app crashes when ever I attempt to type on UISearchDisplayController
For some reason sometimes my app will just hang with the following code: NSError
For some reason, attaching an internal OnClickListener to an ImageButton crashes the app. For
I've got an ASP.NET app that requires Windows Integrated Security. For some reason, I
I'm now developing an app on iOS. With some reason, I want to use
for some sick reason, my check productIDs[addIndex] = allProductIDs[lastProductFoundIndex + i]; causes my app
I am trying to use log4net in a VB.NET app for some unknown reason
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
For some reason my blackberry application crashes whenever i try to display a bitmap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.