应用程序不会在方向更改时保留对象的状态

时间:2016-06-21 01:51:41

标签: java android

我有这个应用程序显示一个电阻器(复合图像),下面是四个数字选择器,供用户选择值...一切正常,直到方向改变,并且用户界面不重新创建,我希望保留数字选择器和图像视图的所有显示值...我的保存方法出错了什么?

这是我的主要活动课程

public class MainActivity extends Activity {

    RelativeLayout rl;
    TextView resistorValue;
    MyImageView bandOne, bandTwo, bandThree, tolerance;
    int figureOne;
    int figureTwo;
    int multiplier;
    String toleranceRate;
    String resistanceDisplayed;
    MyNumberPicker pickerOne;
    MyNumberPicker pickerTwo;
    MyNumberPicker pickerThree;
    MyNumberPicker pickerTolerance;
    SharedPreferences prefs;
    SharedPreferences.Editor editor;
    String colorSelected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //gets shared preferences
        prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
        editor = prefs.edit();

        //initialize the user interface objects
        rl = (RelativeLayout)findViewById(R.id.main_layout);
        bandOne = (MyImageView)findViewById(R.id.colorOne);
        bandTwo = (MyImageView)findViewById(R.id.colorTwo);
        bandThree = (MyImageView)findViewById(R.id.colorThree);
        tolerance = (MyImageView)findViewById(R.id.Tolerance);
        resistorValue = (TextView)findViewById(R.id.resValue);

        pickerOne = (MyNumberPicker)findViewById(R.id.bandOne);
        pickerTwo = (MyNumberPicker)findViewById(R.id.bandTwo);
        pickerThree = (MyNumberPicker)findViewById(R.id.bandThree);
        pickerTolerance = (MyNumberPicker)findViewById(R.id.bandTolerance);

        //checks for shared preferences 
        colorSelected = prefs.getString("background_color", "WHITE");
        if(colorSelected.equals("RED")){
            rl.setBackgroundColor(Color.RED);
        }else if(colorSelected.equals("GREEN")){
            rl.setBackgroundColor(Color.GREEN);
        }else if(colorSelected.equals("BLUE")){
            rl.setBackgroundColor(Color.BLUE);
        }else if(colorSelected.equals("BLACK")){
            rl.setBackgroundColor(Color.BLACK);
        }else{
            rl.setBackgroundColor(Color.WHITE);
        }

        if(savedInstanceState != null){
            bandOne = (MyImageView)savedInstanceState.getSerializable("bandOne");
            bandTwo = (MyImageView)savedInstanceState.getSerializable("bandTwo");
            bandThree = (MyImageView)savedInstanceState.getSerializable("bandThree");
            tolerance = (MyImageView)savedInstanceState.getSerializable("tolerance");
            pickerOne = (MyNumberPicker)savedInstanceState.getSerializable("pickerOne");
            pickerTwo = (MyNumberPicker)savedInstanceState.getSerializable("pickerTwo");
            pickerThree = (MyNumberPicker)savedInstanceState.getSerializable("pickerThree");

        }

        pickerOne.setMinValue(0);
        pickerOne.setMaxValue(9);
        pickerTwo.setMinValue(0);
        pickerTwo.setMaxValue(9);
        pickerThree.setMinValue(0);
        pickerThree.setMaxValue(9);
        pickerTolerance.setMinValue(0);
        pickerTolerance.setMaxValue(3);

        //sets the displayed values of the number pickers
        pickerOne.setDisplayedValues(new String[] { "Black", "Brown", "Red",
                "Orange", "Yellow", "Green", "Blue", "Violet", "Grey",
                "White" });
        pickerTwo.setDisplayedValues(new String[] { "Black", "Brown", "Red",
                "Orange", "Yellow", "Green", "Blue", "Violet", "Grey",
                "White" });
        pickerThree.setDisplayedValues(new String[] { "Black", "Brown", "Red",
                "Orange", "Yellow", "Green", "Blue", "Violet", "Grey",
                "White" });

        pickerTolerance.setDisplayedValues(new String[] { "Brown", "Red", "Gold", "Silver"});

        //triggered when numberPickers value are changed
        pickerOne.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                switch (newVal) {
                case 0:
                    bandOne.setImageResource(R.drawable.black);
                    bandOne.setTag(Integer.valueOf(R.drawable.black));
                    figureOne = 0;
                    break;
                case 1:
                    bandOne.setImageResource(R.drawable.brown);
                    bandOne.setTag(Integer.valueOf(R.drawable.brown));
                    figureOne = 1;
                    break;
                case 2:
                    bandOne.setImageResource(R.drawable.red);
                    figureOne = 2;
                    break;
                case 3:
                    bandOne.setImageResource(R.drawable.orange);
                    figureOne = 3;
                    break;
                case 4:
                    bandOne.setImageResource(R.drawable.yellow);
                    figureOne = 4;
                    break;
                case 5:
                    bandOne.setImageResource(R.drawable.green);
                    figureOne = 5;
                    break;
                case 6:
                    bandOne.setImageResource(R.drawable.blue);
                    figureOne = 6;
                    break;
                case 7:
                    bandOne.setImageResource(R.drawable.violet);
                    figureOne = 7;
                    break;
                case 8:
                    bandOne.setImageResource(R.drawable.grey);
                    figureOne = 8;
                    break;
                case 9:
                    bandOne.setImageResource(R.drawable.white);
                    figureOne = 9;
                    break;
                }
                calculateResistorValue();
            }
        });

        pickerTwo.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                switch (newVal) {
                case 0:
                    bandTwo.setImageResource(R.drawable.black);
                    figureTwo = 0;
                    break;
                case 1:
                    bandTwo.setImageResource(R.drawable.brown);
                    figureTwo = 1;
                    break;
                case 2:
                    bandTwo.setImageResource(R.drawable.red);
                    figureTwo = 2;
                    break;
                case 3:
                    bandTwo.setImageResource(R.drawable.orange);
                    figureTwo = 3;
                    break;
                case 4:
                    bandTwo.setImageResource(R.drawable.yellow);
                    figureTwo = 4;
                    break;
                case 5:
                    bandTwo.setImageResource(R.drawable.green);
                    figureTwo = 5;
                    break;
                case 6:
                    bandTwo.setImageResource(R.drawable.blue);
                    figureTwo = 6;
                    break;
                case 7:
                    bandTwo.setImageResource(R.drawable.violet);
                    figureTwo = 7;
                    break;
                case 8:
                    bandTwo.setImageResource(R.drawable.grey);
                    figureTwo = 8;
                    break;
                case 9:
                    bandTwo.setImageResource(R.drawable.white);
                    figureTwo = 9;
                    break;
                }
                calculateResistorValue();
            }
        });

        pickerThree.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                switch (newVal) {
                case 0:
                    bandThree.setImageResource(R.drawable.black);
                    multiplier = 0;
                    break;
                case 1:
                    bandThree.setImageResource(R.drawable.brown);
                    multiplier = 1;
                    break;
                case 2:
                    bandThree.setImageResource(R.drawable.red);
                    multiplier = 2;
                    break;
                case 3:
                    bandThree.setImageResource(R.drawable.orange);
                    multiplier = 3;
                    break;
                case 4:
                    bandThree.setImageResource(R.drawable.yellow);
                    multiplier = 4;
                    break;
                case 5:
                    bandThree.setImageResource(R.drawable.green);
                    multiplier = 5;
                    break;
                case 6:
                    bandThree.setImageResource(R.drawable.blue);
                    multiplier = 6;
                    break;
                case 7:
                    bandThree.setImageResource(R.drawable.violet);
                    multiplier = 7;
                    break;
                case 8:
                    bandThree.setImageResource(R.drawable.grey);
                    multiplier = 8;
                    break;
                case 9:
                    bandThree.setImageResource(R.drawable.white);
                    multiplier = 9;
                    break;
                }
                calculateResistorValue();
            }
        });

        pickerTolerance.setOnValueChangedListener(new NumberPicker.OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal){
                switch(newVal){
                case 0:
                    tolerance.setImageResource(R.drawable.brown);
                    toleranceRate = "1%";
                    break;
                case 1:
                    tolerance.setImageResource(R.drawable.red);
                    toleranceRate = "2%";
                    break;
                case 2:
                    tolerance.setImageResource(R.drawable.gold);
                    toleranceRate = "5%";
                    break;
                case 3:
                    tolerance.setImageResource(R.drawable.silver);
                    toleranceRate = "10%";
                    break;  
                }
                calculateResistorValue();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.preferences_background_color) {
            CharSequence colors[] = new CharSequence[] {"red","green", "blue", "black"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Pick a color");
            builder.setItems(colors, new DialogInterface.OnClickListener() {

                //opens a dialog that let the user choose the background color and saves it to shared preferences
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    switch(which){
                    case 0:
                        rl.setBackgroundColor(Color.RED);
                        colorSelected="RED";
                        editor.putString("background_color", colorSelected);
                        editor.commit();
                        break;
                    case 1:
                        rl.setBackgroundColor(Color.GREEN);
                        colorSelected="GREEN";
                        editor.putString("background_color", colorSelected);
                        editor.commit();
                        break;
                    case 2:
                        rl.setBackgroundColor(Color.BLUE);
                        colorSelected="BLUE";
                        editor.putString("background_color", colorSelected);
                        editor.commit();
                        break;
                    case 3:
                        rl.setBackgroundColor(Color.BLACK);
                        colorSelected="BLACK";
                        editor.putString("background_color", colorSelected);
                        editor.commit();
                        break;
                    default:
                        rl.setBackgroundColor(Color.WHITE);
                        break;
                    }
                }
            });
            builder.show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putSerializable("bandOne", bandOne);
        outState.putSerializable("bandTwo", bandTwo);
        outState.putSerializable("bandThree", bandThree);
        outState.putSerializable("tolerance", tolerance);
        outState.putSerializable("pickerOne", pickerOne);
        outState.putSerializable("pickerTwo", pickerTwo);
        outState.putSerializable("pickerThree", pickerThree);
        super.onSaveInstanceState(outState);
    }

    //calculates the resistor value and sets text of textView
    public void calculateResistorValue(){

        double resValue = ((10 * figureOne) + figureTwo) * Math.pow(10, multiplier) / 1000;
        String resMessage = null;
        if(resValue >= (10^6)){
            resValue /= (10^6);
            resMessage= Integer.toString((int) resValue) + "  M Ohms";
        }else if(resValue >= (10^3)){
            resValue /= (10^3);
            resMessage = Integer.toString((int) resValue) + "  k Ohms";
        }else{
            resMessage = Integer.toString((int) resValue) + "  Ohms";
        }
        resistorValue.setText(resMessage + "  " + toleranceRate );
    }       
}

此外,我还有自定义类My Number Picker和My Image View(并且它们可以正常工作)和XML文件,我相信错误是在我的保存方法或On Create方法中,一旦对象被反序列化。

![电阻图片] http://www.electronics123.com/page/calculate-resistor-values

2 个答案:

答案 0 :(得分:0)

您正在尝试将过多数据保存到捆绑包中。只保存您需要的内容,可能将NumberPicker更改的值存储到主活动类的成员中,然后使用这些成员保存数据。您可以将NumberPicker.OnValueChangedListener中的逻辑移动到公共函数,并在方向更改后拥有活动实例时调用此函数onCreate。您只会从包中加载简单的原始值,而不是复杂的视图对象。

编辑哎呀你已经使用了一个成员,只需创建一个函数并从包中保存/加载这些值并调用该函数来恢复数字选择器!

答案 1 :(得分:0)

尝试保存值:

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("figureOne", pickerOne);
        outState.putInt("figureTwo", pickerTwo);
        outState.putInt("multiplier", multiplier);
        outState.putInt("tolerance", pickerTolerance.getValue());
        super.onSaveInstanceState(outState);
    }

在onCreate()中,拥有在所有侦听器设置后恢复值的代码。恢复如下值的代码:

        if(savedInstanceState != null){
            pickerOne.setValue(savedInstanceState.getInt("figureOne"));
            pickerTwo.setValue(savedInstanceState.getInt("figureTwo"));
            pickerThree.setValue(savedInstanceState.getInt("multiplier");
            pickerTolerance.setValue(savedInstanceState.getInt("tolerance"));
        }