2011-12-17 115 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