2011-12-17 168 views
0

當我在模擬器上運行我的應用程序時,它運行正常,沒有問題發生。但是當我在手機上運行它時,出現的問題「意外停止」。當在手機上運行時,應用程序「意外停止」

在我的申請: 主要活動(背景)可以交換到4個活動:客廳,DiningRoom,Wc,車庫。當我嘗試去DiningRoom活動時,問題「意外停止」。

這是我的代碼:

1.背景

package com.example.background; 



import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import at.abraxas.amarino.Amarino; 


public class BackgroundActivity extends Activity { 

    private Button Wc, Dining_Room, Living_Room, Garage; 
    private Intent D_intent, L_intent, G_intent, W_intent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //Amarino.connect(this, DEVICE_ADDRESS); 

     Living_Room = (Button) findViewById(R.id.living); 
     //Living_Room.setBackgroundColor(Color.TRANSPARENT); 

     Living_Room.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       L_intent = new Intent(view.getContext(), LivingRoom.class); 
       startActivityForResult(L_intent, 0); 
      } 

     }); 

     Dining_Room = (Button) findViewById(R.id.dining); 
     // Dining_Room.setBackgroundColor(Color.TRANSPARENT); 

     Dining_Room.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       D_intent = new Intent(view.getContext(), DiningRoom.class); 
       startActivityForResult(D_intent, 0); 
      } 

     }); 

     Wc = (Button) findViewById(R.id.wc); 
     //Wc.setBackgroundColor(Color.TRANSPARENT); 

     Wc.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       W_intent = new Intent(view.getContext(), Wc.class); 
       startActivityForResult(W_intent, 0); 
      } 

     }); 

     Garage = (Button) findViewById(R.id.garage); 
     // Garage.setBackgroundColor(Color.TRANSPARENT); 

     Garage.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       G_intent = new Intent(view.getContext(), Garage.class); 
       startActivityForResult(G_intent, 0); 
      } 

     }); 


    } 


} 

2.DiningRoom

package com.example.background; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.os.Bundle; 
    import android.preference.PreferenceManager; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.CompoundButton; 
    import android.widget.CompoundButton.OnCheckedChangeListener; 
    import android.widget.SeekBar; 
    import android.widget.SeekBar.OnSeekBarChangeListener; 
    import android.widget.ToggleButton; 
    import at.abraxas.amarino.Amarino; 


    public class DiningRoom extends Activity implements OnCheckedChangeListener, OnSeekBarChangeListener{ 


     private static final String DEVICE_ADDRESS = "00:06:66:43:9B:56"; 


     final int DELAY = 150; 

     ToggleButton button; 
     SeekBar seekbar1; 
     SeekBar seekbar2; 

     boolean light2; 

     int light1; 
     int fan; 
     long lastchange; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.diningroom); 


       //back ve trang chinh 
       Button backhome = (Button) findViewById(R.id.D_backhome); 
       backhome.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         Intent intent = new Intent(); 
         setResult(RESULT_OK, intent); 
         finish(); 
        } 

       }); 


       Amarino.connect(this, DEVICE_ADDRESS); 

       button = (ToggleButton) findViewById(R.id.Dbutton); 
       seekbar1 = (SeekBar) findViewById(R.id.Dseekbar1); 
       seekbar2 = (SeekBar) findViewById(R.id.Dseekbar2); 

       button.setOnCheckedChangeListener(this); 
       seekbar1.setOnSeekBarChangeListener(this); 
       seekbar2.setOnSeekBarChangeListener(this); 

     } 



     //---START---------------------------------------------------------------------------------------------- 

     @Override 
     protected void onStart(){ 

      super.onStart(); 

      // load last state 
      SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); 

      light2 = pref.getBoolean("light2", true); 
      light1 = pref.getInt("light1", 0); 
      fan = pref.getInt("fan", 0); 

      button.setChecked(light2); 
      seekbar1.setProgress(light1); 
      seekbar2.setProgress(fan); 

      new Thread(){ 
       public void run(){ 
        try{ 
         Thread.sleep(6000); 
        }catch (InterruptedException e) {} 

        update_light2(); 
        update_light1_fan(); 
       } 
      }.start(); 
     } 


     //STOP-------------------------------------------------------------------------------- 
     @Override 
     protected void onStop(){ 

      super.onStop(); 
      PreferenceManager.getDefaultSharedPreferences(this) 
      .edit() 
       .putBoolean("light2", light2) 
       .putInt("light1", light1) 
       .putInt("fan", fan) 
      .commit(); 

      Amarino.disconnect(this, DEVICE_ADDRESS); 
     } 

     //UPDATE LIGHT2-------------------------------------------------------------------------------- 
     private void Update_Light2_State(final CompoundButton button){ 

        light2 = button.isChecked(); 
        update_light2(); 
     } 


     private void update_light2(){ 

      int a; 

      a = (light2 == true)? 255:0; 
      Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'o', a); 
     } 

     //UPDATE LIGHT1 FAN------------------------------------------------------------------------------------- 

     private void Update_Light1_Fan_State(final SeekBar seekbar){ 

      switch (seekbar.getId()){ 
      case R.id.Dseekbar1: 
       light1 = seekbar.getProgress(); 
       update_light1(); 
       break; 
      case R.id.Dseekbar2: 
       fan = seekbar.getProgress(); 
       update_fan(); 
       break; 
      } 

     } 


     private void update_light1_fan(){ 

      update_light1(); 
      update_fan(); 
     } 


     private void update_light1(){ 

      Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', light1); 
     } 
     private void update_fan(){ 

      Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'q', fan); 
     } 




     //---TRACE-------------------------------------------------------------------------------------- 


     @Override 
     public void onCheckedChanged(CompoundButton button, boolean isChecked) { 

      if (System.currentTimeMillis() - lastchange > DELAY){ 
       Update_Light2_State(button); 
       lastchange = System.currentTimeMillis(); 
      } 

     } 

     @Override 
     public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) { 

      if (System.currentTimeMillis() - lastchange > DELAY){ 
       Update_Light1_Fan_State(seekbar); 
       lastchange = System.currentTimeMillis(); 
      } 

     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekbar) { 

      lastchange = System.currentTimeMillis(); 
     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekbar) { 

      Update_Light1_Fan_State(seekbar); 
     } 
    } 

3.Logcat

FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.background/com.example.background.DiningRoom}: java.lang.ClassCastException: 

java.lang.Boolean 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4633) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.ClassCastException: java.lang.Boolean 
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2721) 
at com.example.background.DiningRoom.onStart(DiningRoom.java:80) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
at android.app.Activity.performStart(Activity.java:3781) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642) 
... 11 more 
Force finishing activity com.example.background/.DiningRoom 
Force finishing activity com.example.background/.BackgroundActivity 

我也嘗試寫另一個應用程序類似於上面的ap摺疊。但我只有一個子活動:DiningRoom,然後在手機上運行良好。

的時候我只有一個子活動的背景

[CODE]

public class Test1Activity extends Activity { 

    private Button Dining_Room; 
    private Intent D_intent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Dining_Room = (Button) findViewById(R.id.dining); 
     // Dining_Room.setBackgroundColor(Color.TRANSPARENT); 

     Dining_Room.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       D_intent = new Intent(view.getContext(), Dining.class); 
       startActivityForResult(D_intent, 0); 
      } 

     }); 

    } 


} 

[/ CODE]

回答

0

的logcat仔細閱讀表明:

Caused by: java.lang.ClassCastException: java.lang.Boolean 

在android.app.ContextImpl $ SharedPreferencesImpl.getInt(ContextImpl.java:2721) 在com.example.background.DiningRoom.onStart(DiningRoom.java:80)

這意味着,共享偏好是布爾而不是在線路INT 80.

PS:我開發輕量依賴注入框架Android,它已經涵蓋了 加載/保存偏好。這裏只是抓住它:

https://github.com/ko5tik/andject

+0

「light1」是int。問題在於使用相同的代碼「DiningRoom」,但應用程序在它只有一個子活動時運行並且在應用程序有四個子活動時出錯 – user1062335 2011-12-17 19:31:57

0

我猜想,在你的共享偏好值「光線1」並沒有保存爲一個整數(但作爲一個布爾值)。 從docs

拋出ClassCastException - 如果沒有與此名稱 不是一個整數的偏好。

+0

「light1」爲int。問題是使用相同的代碼「DiningRoom」,但是應用程序在它只有一個子活動時運行並且在應用程序有四個子活動時出錯 – user1062335 2011-12-17 19:26:09

+0

它也在模擬器上運行,當我運行時只是「停止意外」在電話 – user1062335 2011-12-17 19:31:25

+0

也許在某個活動中,您不小心將布爾值保存爲「light1」(而不是light2也許?) – Jave 2011-12-17 19:34:11