启动活动时值变为0

时间:2015-03-05 13:44:44

标签: android android-intent android-studio

我的应用程序允许用户生成卡路里值(主要活动2)并将其传递给主要活动,以便可以显示,当尝试添加多餐时,它不会添加先前的值并且新值已通过有意图并将其显示在主要活动中,为什么会这样做?任何帮助将不胜感激,解释更多。

主要活动

public class MainActivity extends ActionBarActivity {



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

        TextView calories = (TextView)findViewById(R.id.overall);
        String previouscalories = calories.getText().toString();
        int numold = Integer.parseInt(previouscalories);

        Intent intent = getIntent();
        String newcalories = "";
        if (intent.hasExtra("passedvalue"))
        {
            newcalories = intent.getExtras().getString("passedvalue");

        } else {
            newcalories = "0";  
        }

        int numnew = Integer.parseInt(newcalories);
        int overall = numnew + numold;
        calories.setText("Current Calories:"+ overall);

        Button add = (Button)findViewById(R.id.add_meal);
        Button reset = (Button)findViewById(R.id.reset);
        Button about = (Button)findViewById(R.id.about);

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
                        startActivity(intent);
            }
        });


        about.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),about.class);
                startActivity(intent);
            }
        });
    }





    }

主要活动2

public class MainActivity2 extends ActionBarActivity {


    int sub_weight = 0;

    EditText weight;
    TextView calories;
    Button display, save;


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


        weight = (EditText)findViewById(R.id.edit_weight);
        calories = (TextView)findViewById(R.id.cal_total);
        display = (Button)findViewById(R.id.display);

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView calories = (TextView)findViewById(R.id.cal_total);
                String g = calories.getText().toString();
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("passedvalue", g );
                startActivity(intent);
            }
        });

    }
    public void onRadioButtonClicked(View v){

        boolean checked = ((RadioButton) v).isChecked();

        switch (v.getId()) {
            case R.id.radiopork:
                if (checked)
                    sub_weight = sub_weight + 2;
                break;
            case R.id.radiochicken:
                if (checked)
                    sub_weight = sub_weight + 7;
                break;
            case R.id.radiobeef:
                if (checked)
                    sub_weight = sub_weight + 9;
                break;
            case R.id.radiosalmon:
                if (checked)
                    sub_weight = sub_weight + 13;
                break;
            case R.id.radiocod:
                if (checked)
                    sub_weight = sub_weight + 17;
                break;
            case R.id.radiocereal:
                if (checked)
                    sub_weight = sub_weight + 18;
                break;
            case R.id.radioporridge:
                if (checked)
                    sub_weight = sub_weight + 23;
                break;
            case R.id.radiotoast:
                if (checked)
                    sub_weight = sub_weight + 26;
                break;
            case R.id.radiocrisps:
                if (checked)
                    sub_weight = sub_weight + 29;
                break;
            case R.id.radionoodle:
                if (checked)
                    sub_weight = sub_weight + 33;
                break;


        }

        }
    public void display_calories(View v){

        String m = weight.getText().toString();
        int x =  Integer.parseInt(m);
        int y = x * sub_weight;
        calories.setText(y+"");
        sub_weight = 0;

    }


}

2 个答案:

答案 0 :(得分:0)

TextView calories = (TextView)findViewById(R.id.overall);

每次创建活动时,除非您以这种方式对其进行编码,否则它不知道其先前的状态,因此除非您自己在xml中添加了某些内容作为默认值,否则calories将没有任何值(不会是用户之前输入的内容。)

如果要保存用户在创建/删除活动之间输入的内容,请将第二个活动的结果发送回第一个活动(请参阅here)或将值保存在sharedPreference上(或数据库,但对于单个值可能有点过分)。

答案 1 :(得分:0)

将以前的值保存在静态变量中,以便保持值可重用。

public class MainActivity extends ActionBarActivity {
private static String previouscalories;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView calories = (TextView)findViewById(R.id.overall);
        int numold = Integer.parseInt(previouscalories);

        Intent intent = getIntent();
        String newcalories = "";
        if (intent.hasExtra("passedvalue"))
        {
            newcalories = intent.getExtras().getString("passedvalue");

        } else {
            newcalories = "0";  
        }

        int numnew = Integer.parseInt(newcalories);
        int overall = numnew + numold;
        previouscalories = String.valueOf(overall);
        calories.setText("Current Calories:"+ overall);