ANDROID: the application just  "stop unexpectedly" when run on the
phone


When I run my application on the emulator, it run right and no problem
happens. But when I run it on the phone, a problem appear "stop
unexpectedly".

In my application:
The main activity (Background) can exchange to 4 activities:
LivingRoom, DiningRoom, Wc, Garage. The problem "stop unexpectedly"
just happens when i try to go to the DiningRoom activity.

This is my code :

1.Background
[CODE]

    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);
                }

            });


        }


    }

[/CODE]

2. DiningRoom

[CODE]



     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);
                }
        }

[/CODE]


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



I also try to wrote another application similar to the above
application. But I just have only 1 sub activity: DiningRoom, then it
ran well on the phone.

Please give me some suggestions. Thanks so much

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to