
我一直在努力使它起作用,我想要这样,以便可以将两个checkChanged放在一个OnCheckedChanged方法中.到目前为止,我没有任何帮助,但是像以前一样,只有一种情况,而没有switch语句.请您看看我的代码,看看如何修复它才能使它工作?
这是代码:
public class MainActivity extends Activity implements OnClickListener, OnCheckedchangelistener { button sensorbutton; button newScreen; Switch vibratetoggle; Switch lightsToggle; notificationmanager nm1; notificationmanager nm2; static final int uniqueID1 = 12345; static final int uniqueID2 = 54321; OnCheckedchangelistener vibrateListener; OnCheckedchangelistener lightsListener; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); sensorbutton = (button)this.findVIEwByID(R.ID.sensorbutton); sensorbutton.setonClickListener(this); newScreen = (button)this.findVIEwByID(R.ID.newScreen); newScreen.setonClickListener(this); nm1 = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); nm1.cancel(uniqueID1); nm2 = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); nm2.cancel(uniqueID2); vibratetoggle = (Switch)this.findVIEwByID(R.ID.switch1); vibratetoggle.setonCheckedchangelistener(vibrateListener); lightsToggle = (Switch)this.findVIEwByID(R.ID.lightSwitch); lightsToggle.setonCheckedchangelistener(lightsListener); } @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) { // Todo auto-generated method stub switch(buttonVIEw.getID()){ case R.ID.switch1: Toast.makeText(this, "Vibrate Notification is " + (isChecked ? "ON" : "OFF"), Toast.LENGTH_SHORT).show(); if (isChecked) { // The toggle is enabled Intent vibrateIntent = new Intent(this, MainActivity.class); PendingIntent vibratePi = PendingIntent.getActivity(this, 0, vibrateIntent, 0); NotificationCompat.Builder vibrateN = new NotificationCompat.Builder(this); vibrateN.setContentIntent(vibratePi); vibrateN.setDefaults(Notification.DEFAulT_VIBRATE); Notification vn = vibrateN.build(); nm2.notify(uniqueID2, vn); } else { // The toggle is Disabled } case R.ID.lightSwitch: Toast.makeText(this, "lights Notification is " + (isChecked ? "ON" : "OFF"), Toast.LENGTH_SHORT).show(); if (isChecked) { // The toggle is enabled Intent lightsIntent = new Intent(this, MainActivity.class); PendingIntent lightsPi = PendingIntent.getActivity(this, 0, lightsIntent, 0); NotificationCompat.Builder lightsN = new NotificationCompat.Builder(this); lightsN.setContentIntent(lightsPi); lightsN.setDefaults(Notification.DEFAulT_liGHTS); Notification ln = lightsN.build(); nm2.notify(uniqueID2, ln); } else { // The toggle is Disabled } break; } }这是我得到的日志猫:
03-14 16:46:21.139: D/dalvikvm(532): Not late-enabling CheckJNI (already on)03-14 16:46:22.218: I/dalvikvm(532): threadID=3: reacting to signal 303-14 16:46:22.338: I/dalvikvm(532): Wrote stack traces to '/data/anr/traces.txt'03-14 16:46:22.728: I/dalvikvm(532): threadID=3: reacting to signal 303-14 16:46:22.739: I/dalvikvm(532): Wrote stack traces to '/data/anr/traces.txt'03-14 16:46:23.238: I/dalvikvm(532): threadID=3: reacting to signal 303-14 16:46:23.278: I/dalvikvm(532): Wrote stack traces to '/data/anr/traces.txt'03-14 16:46:23.509: D/gralloc_goldfish(532): Emulator without GPU emulation detected.03-14 16:46:25.258: D/AndroIDRuntime(532): Shutting down VM03-14 16:46:25.258: W/dalvikvm(532): threadID=1: thread exiting with uncaught exception (group=0x409c01f8)03-14 16:46:25.288: E/AndroIDRuntime(532): FATAL EXCEPTION: main03-14 16:46:25.288: E/AndroIDRuntime(532): java.lang.IllegalStateException: Could not find a method onSwitchToggle(VIEw) in the activity class com.example.sensor.MainActivity for onClick handler on vIEw class androID.Widget.Switch with ID 'switch1'03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.vIEw.VIEw.onClick(VIEw.java:3031)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.vIEw.VIEw.performClick(VIEw.java:3511)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.Widget.Compoundbutton.performClick(Compoundbutton.java:100)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:14105)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.os.Handler.handleCallback(Handler.java:605)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.os.Handler.dispatchMessage(Handler.java:92)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.os.Looper.loop(Looper.java:137)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.app.ActivityThread.main(ActivityThread.java:4424)03-14 16:46:25.288: E/AndroIDRuntime(532): at java.lang.reflect.Method.invokeNative(Native Method)03-14 16:46:25.288: E/AndroIDRuntime(532): at java.lang.reflect.Method.invoke(Method.java:511)03-14 16:46:25.288: E/AndroIDRuntime(532): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)03-14 16:46:25.288: E/AndroIDRuntime(532): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)03-14 16:46:25.288: E/AndroIDRuntime(532): at dalvik.system.NativeStart.main(Native Method)03-14 16:46:25.288: E/AndroIDRuntime(532): Caused by: java.lang.NoSuchMethodException: onSwitchToggle [class androID.vIEw.VIEw]03-14 16:46:25.288: E/AndroIDRuntime(532): at java.lang.class.getConstructorOrMethod(Class.java:460)03-14 16:46:25.288: E/AndroIDRuntime(532): at java.lang.class.getmethod(Class.java:915)03-14 16:46:25.288: E/AndroIDRuntime(532): at androID.vIEw.VIEw.onClick(VIEw.java:3024)03-14 16:46:25.288: E/AndroIDRuntime(532): ... 12 more03-14 16:46:25.948: I/dalvikvm(532): threadID=3: reacting to signal 303-14 16:46:25.958: I/dalvikvm(532): Wrote stack traces to '/data/anr/traces.txt'解决方法:
如在日志中:
NoSuchMethodException: onSwitchToggle [class androID.vIEw.VIEw]
表示您已在Switch VIEw xml中添加了androID:onClick =“ onSwitchToggle”属性,但是您忘记在Activity代码中定义方法.
因为您要将setonCheckedchangelistener()添加到Activity中的Switch VIEw,所以不需要在XML中为Switch VIEw设置onClick().只需从XML中删除androID:onClick =“ onSwitchToggle”属性即可.
总结以上是内存溢出为你收集整理的java-如何在Android中使用带有’onCheckedChanged’方法的switch语句?全部内容,希望文章能够帮你解决java-如何在Android中使用带有’onCheckedChanged’方法的switch语句?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)