【问题标题】:Why does setOnCheckedChangeListener? cause crash?为什么 setOnCheckedChangeListener?导致崩溃?
【发布时间】:2013-01-27 08:44:03
【问题描述】:

由于某种原因,我的应用程序在启动时崩溃了。这是所有代码和调试输出。在我看来,这与我使用 setOnCheckedChangeListener 的方式有关..

调试输出:

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
        }
    }

}

【问题讨论】:

  • 如果您从以下答案中得到答案,那么您需要接受它。

标签: android uiswitch


【解决方案1】:

您需要在 MainActivity 中添加 implements OnCheckedChangeListener

我让你的代码工作了,见下文....

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
    }
}

【讨论】:

    【解决方案2】:

    您的应用程序崩溃,因为您将侦听器回调投射到您的活动中。 这不是在android中实现回调的方式。

    试试这个。

    public class MainActivity extends Activity implements OnCheckedChangeListener{
    
        @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);    // note this
            }
        }
    
        @override                                    // take a note
         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
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      你想要的只是

      1 个实现

       implement OnCheckedChangeListener
      

      2 改变

       s.setOnCheckedChangeListener((OnCheckedChangeListener) this);
      

          s.setOnCheckedChangeListener(this);
      

      【讨论】:

        【解决方案4】:

        Switch继承了CompoundButton的属性,所以我推荐OnCheckedChangeListener

        mySwitch.setOnCheckedChangeListener(new OnCheckedChangedListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // Do Something
            }
        });
        

        【讨论】:

          【解决方案5】:
          class MainActivity : AppCompatActivity() {
          private var switchCompat: SwitchCompat? = null
          override fun onCreate(savedInstanceState: Bundle?) {
                  super.onCreate(savedInstanceState)
                  setContentView(R.layout.activity_main)
                  setupView()
              }
          
              private fun setupView(){
                  switchCompat = findViewById(R.id.switch_compat)
                  switchCompat?.setOnCheckedChangeListener { buttonView, isChecked ->
                      Toast.makeText(this, ""+isChecked, Toast.LENGTH_SHORT).show()
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2021-06-28
            • 1970-01-01
            • 2012-06-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-03
            相关资源
            最近更新 更多