2013-03-14 72 views
3

我一直在試圖爲年齡來得到這個工作,我想它,這樣我可以在一個OnCheckedChanged方法把兩者的CheckedChanged。到目前爲止,我的工作並不成功,但正如我以前所做的那樣,只有一個案例,沒有切換語句的工作。請你可以看看我的代碼,看看我可以修復它,使其工作?如何在android中使用'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; 
     } 
    } 

這是我得到的logcat:

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 3 
03-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 3 
03-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 3 
03-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 VM 
03-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: main 
03-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$1.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$1.onClick(View.java:3024) 
03-14 16:46:25.288: E/AndroidRuntime(532): ... 12 more 
03-14 16:46:25.948: I/dalvikvm(532): threadid=3: reacting to signal 3 
03-14 16:46:25.958: I/dalvikvm(532): Wrote stack traces to '/data/anr/traces.txt' 
+2

你忘了休息 – njzk2 2013-03-14 16:25:08

+1

@ρяσѕρєяK刪除android:onClick="onSwitchToggle"屬性:你不_must_,(否則這將是無用的),但你真的應該,在大多數情況下, – njzk2 2013-03-14 16:25:45

+0

解決或仍然沒有問題? – 2013-03-14 16:42:25

回答

4

在日誌中:

NoSuchMethodException:onSwitchToggle [類android.view。查看]

表示您在Switch View xml中添加了android:onClick="onSwitchToggle"屬性,但您忘記在Activity代碼中定義方法。

,因爲你要添加setOnCheckedChangeListener到交換機視圖在活動則無需設置裏面的onClick XML進行切換查看剛剛從XML

+0

謝謝!這解決了問題! – 2013-03-14 17:42:14