如何解决这个“方法调用”bundle.getString(“Ans_age”)。equals(R.string.age2)'可能会产生'java.lang.NullPointerException'“

时间:2016-01-20 08:02:42

标签: java android

为什么我总是在if else语句中得到这条消息:方法调用'bundle.getString(“Ans_phy”)。equals(R.string.active)'可能产生'java.lang.NullPointerException'

我的提交按钮也无法正常工作,因为一旦我点击提交按钮,它就会在下一个活动中显示“newWeight”和“calories”。

Review.java

public void setBtnSubmit()
{
    //Create a bundle object to store the bundle added to the intent
    final Bundle bundle = getIntent().getExtras();

    //Get the values out by key
    final String umur = bundle.getString("Ans_age");
    final String textBMI = bundle.getString("Ans_bmi");
    final String aktiviti = bundle.getString("Ans_phy");

final String textCategory = bundle.getString("Ans_category");

    final String txtcf = bundle.getString("cfDisease");

    //Get the textview controls
    final TextView txtage = (TextView) findViewById(R.id.textView5);
    final TextView txtbmi = (TextView) findViewById(R.id.textView7);
    final TextView txtphy = (TextView) findViewById(R.id.textView6);

final TextView txtcategory = (TextView) findViewById(R.id.result);

    //final TextView cf_result = (TextView) findViewById(R.id.result);

    //Button buttonSend = (Button) findViewById(R.id.btnsend);

    //Set the text values of the text controls
    txtage.setText(umur);
    txtbmi.setText(textBMI);
    txtphy.setText(aktiviti);

txtcategory.setText(textCategory);

    //cf_result.setText(txtcf);

    Button btnsubmit = (Button) findViewById(R.id.btnsubmit);
    //Intent intentsubmit = new Intent();

    //intentsubmit.setClass(Review.this, Result.class);
    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //startActivity(intentsubmit);

            Integer calories;
            double weight1, weight2, weight3;
            double newWeight;

            // AGE 41 - 45
            if (bundle.getString("Ans_age").equals(R.string.age1)) // 41 - 45
            {
                weight1 = 0.7;
                if (bundle.getString("Ans_phy").equals(R.string.moderate)) //Sedentary Active
                {
                    weight2 = 0.8;
                    if ((txtcategory.equals("Underweight"))) //Underweight
                    {
                        weight3 = 0.6;
                        newWeight = 0.8 * weight2;
                        calories = 1600;

                        String cf = Double.toString(newWeight);
                        String cal = Integer.toString(calories);


                        Intent intent = new Intent(Review.this, Result.class);
                        Bundle bundle = new Bundle();

                        bundle.putString("Ans_calories", cal);
                        bundle.putString("cfCalories", cf);

                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            }
            else
            if (bundle.getString("Ans_age").equals(R.string.age2)) // 46 - 50
            {
                weight1 = 0.7;
                if (bundle.getString("Ans_phy").equals(R.string.active)) // Moderately Active
                {
                    weight2 = 0.7;
                    if ((txtcategory.equals("Normal"))) // Underweight
                    {
                        weight3 = 0.8;
                        newWeight = 0.9 * weight1;
                        calories = 1800;

                        String cf = Double.toString(newWeight);
                        String cal = Integer.toString(calories);


                        Intent intent = new Intent(Review.this, Result.class);
                        Bundle bundle = new Bundle();

                        bundle.putString("Ans_calories", cal);
                        bundle.putString("cfCalories", cf);

                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            }
            else
            if (bundle.getString("Ans_age").equals(R.string.age2)) // 41 - 45
            {
                weight1 = 0.8;
                if (bundle.getString("Ans_phy").equals(R.string.moderate)) // Active
                {
                    weight2 = 0.8;
                    if ((txtcategory.equals("Obese"))) // Underweight
                    {
                        weight3 = 0.8;
                        newWeight = 0.9 * weight3;
                        calories = 2000;

                        String cf = Double.toString(newWeight);
                        String cal = Integer.toString(calories);


                        Intent intent = new Intent(Review.this, Result.class);
                        Bundle bundle = new Bundle();

                        bundle.putString("Ans_calories", cal);
                        bundle.putString("cfCalories", cf);

                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            }

        }
    });
}

Result.java

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

    Intent intent = getIntent();
    final String cal = intent.getStringExtra("Ans_calories");
    final String cf = intent.getStringExtra("cfCalories");

    final TextView kalori = (TextView) findViewById(R.id.show_calories);
    final TextView keputusan = (TextView) findViewById(R.id.show_result);

    kalori.setText(cal);
    keputusan.setText(cf);
}

3 个答案:

答案 0 :(得分:3)

您可以执行以下操作从Intent获取数据:

Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String yourStirng = extras.getString("yourKeyString");
        }

并尝试将数据置于意图中,您也可以这样做:

 Intent intent = new Intent(getActivity() / this ,YourActivity.class);
        intent.putExtra("YOURKEyString", "Your value string");
        startActivity(intent);

请尝试这一点并希望它有所帮助

答案 1 :(得分:1)

我使用了 if ,然后使用了另一个 getString 方法,该方法获得了两个参数

if (bundle != null) {
            myIp=bundle.getString("ip","");
}

这两个参数 getString 方法描述如下:

返回与给定键关联的值,或者如果为defaultValue,则返回      *对于给定的键或空值,不存在所需类型的映射      *值与给定键明确关联。

的含义,如果第一个参数返回null,则分配一个默认值 但您可以决定默认值是否与您的情况相关

答案 2 :(得分:0)

String s = bundle.getString("Ans_age");
if(s.length() != 0 && s..equals(R.string.age1)){
 ... continue

OR

Integer age = Integer.valueOf(bundle.getString("Ans_age")); 
//surround the above with numberFormatException
if(age < 46){ //you can add more checks like `&& age >39`
....
}else if(age > 46){ //you can add more checks like `&& age < 51
...