应用程序在手机上运行时“意外停止”

时间:2011-12-17 18:42:28

标签: android

当我在模拟器上运行我的应用程序时,它运行正常,没有问题发生。但是当我在手机上运行时,出现问题“意外停止”。

在我的申请中: 主要活动(背景)可以交换4个活动:LivingRoom,DiningRoom,Wc,Garage。当我尝试去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

我还尝试编写与上述应用程序类似的其他应用程序。但我只有一个子活动: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]

2 个答案:

答案 0 :(得分: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行的int。

PS:我为android开发轻量级依赖注入框架,它已经涵盖了 加载/保存首选项。抓住它吧:

https://github.com/ko5tik/andject

答案 1 :(得分:0)

我猜你的共享偏好中的值“light1”没有保存为整数(而是作为布尔值)。 来自docs

  

如果存在具有此名称的首选项,则抛出ClassCastException   不是一个int。