2011-12-17 8 views
0

私のエミュレータでアプリケーションを実行すると、正常に動作し、問題は発生しません。しかし、私はそれを電話で実行すると、問題は "予期せず停止"と表示されます。アプリケーションが電話で実行されると「予期せず停止する」

私のアプリケーション: 主なアクティビティ(背景)は、LivingRoom、DiningRoom、Wc、Garageの4つのアクティビティに交換できます。私がDiningRoomアクティビティに行くときに問題が「予期せず停止する」ということが起こります。

これは私のコードです:私はまた別のアプリケーションを書いしよう

1.Background

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と同様襞付け。しかし、私はちょうど1つのサブアクティビティを持っています:DiningRoom、それは電話でうまく走った。

私はちょうど一つのサブ活性

を有する背景[コード]

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)

このことは、80

はPSの代わりにライン上のINTのブールましたアンドロイド、それはすでに の設定を読み込み/保存しています。ただ、ここでそれをつかむ:

https://github.com/ko5tik/andject

+0

"light1"はintです。問題は同じコード "DiningRoom"であったが、アプリケーションがサブアクティビティーを1つしか持たず、アプリケーションに4つのサブアクティビティーがあるときにエラーが発生したときにアプリケーションが実行された。 – user1062335

0

は、私はあなたの共有設定で値「light1」は整数として保存されていなかったことを推測する(しかし、booleanとして)。 docsから : はint型ではないことを、この名前の優先順位がある場合

は、ClassCastExceptionがスローされます。

+0

"light1"がintであった。問題は同じコード "DiningRoom"であったが、アプリケーションが1つのサブアクティビティを持つときにアプリケーションが実行され、アプリケーションに4つのサブアクティビティがあるときにエラーが発生した – user1062335

+0

これはエミュレータでも実行され、 – user1062335

+0

たぶん、いずれかのアクティビティのどこかに、あなたは誤って "light1"(light2の代わりに?)のブール値を保存します。 – Jave