2016-07-06 5 views
2

BMI 계산기 앱을 만들려고하는데 의도와 메소드 getIntExtra()를 사용하는 데 문제가있는 것 같습니다. 항상 기본값을 얻지 만 결국 다른 활동에서 전달한 가치. 다음은 첫 번째 활동에 대한 내 코드입니다안드로이드 BMI 계산기 응용 프로그램의 의도가 잘못되었습니다

calculate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getApplicationContext(), SecondActivity.class); 

      int weight = Integer.parseInt(weightText.getText().toString()); 
      int height = Integer.parseInt(heightText.getText().toString()); 

      intent.putExtra(USER_WEIGHT_EXTRA, weight); 
      intent.putExtra(USER_HEIGHT_EXTRA, height); 
      startActivity(intent); 
     } 
    }); 

두 번째 활동 더블 height2 = (double)height/100 이유는 m로 cm로 변환하는 것입니다.

result = (TextView)findViewById(R.id.result); 
    todo = (TextView) findViewById(R.id.todo); 

    Intent intent = getIntent(); 

    int weight = intent.getIntExtra("USER_WEIGHT_EXTRA", extraInt); 
    int height = intent.getIntExtra("USER_HEIGHT_EXTRA", extraInt); 

    double height2 = (double)height/100; 
    double BMI = (weight*1.0)/(height2*height2); 


    if (BMI < 20.0) { 
     result.setText("You are: UNDERWEIGHT"); 
     todo.setText("You Should EAT MORE"); 
    } else if (BMI > 20.0 && BMI < 25.0) { 
     result.setText("You are: NORMAL WEIGHT"); 
     todo.setText("You Should keep STAYING HEALTHY"); 
    } else if (BMI > 25) { 
     result.setText("You are: OVERWEIGHT"); 
     todo.setText("You Should EXERCISE MORE"); 
    } 
} 

정말이 문제에 착수했습니다. 정말 고마워요! 첫 번째 활동에서

+0

에 대한

Intent in = new Intent(this, OtherActivity.class); in.putExtra("USER_WEIGHT_EXTRA", weightText.getText().toString()); in.putExtra("USER_HEIGHT_EXTRA", heightText.getText().toString()); startActivity(in);' 

'USER_WEIGHT_EXTRA = "USER_WEIGHT_EXTRA은"'합니까? 높이도 마찬가지다. 첫 번째 활동에서 변수를 사용하고 두 번째 문자열 리터럴을 사용하면 문제가 될 수 있습니다. – ElefantPhace

+0

첫 번째 활동의 USER_WEIGHT_EXTRA는 문자열 "USER_WEIGHT_EXTRA" – hwhong

+0

을 보유하는 문자열 변수이므로 내 질문에 대한 대답은 yes입니다. – ElefantPhace

답변

0

otherActivity

Bundle bundle = getIntent().getExtras(); 

     int weight = Integer.parseInt(bundle.getString("USER_WEIGHT_EXTRA")); 
     int height = Integer.parseInt(bundle.getString("USER_HEIGHT_EXTRA")); 
+2

고마워요! 그것은 일했다! – hwhong

+0

기꺼이 받아 들여서 실을 닫으십시오. –

관련 문제