if(String == null或String =="")没有激活

时间:2016-07-15 07:47:05

标签: android

我遇到了这个问题,我有三个if语句,每个语句都有IF String == null或String ==""甚至getText也没有给出任何价值,这些都没有激活。

 @Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {

        case R.id.action_two:

            final String hei = h.getText().toString();
            final String wei = w.getText().toString();
            String waist = wc.getText().toString();


            if (hei == null || wei == null ){
                final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
                checker.setText("Please, put your information in these spots!");
                checker.startAnimation(animationFadeIn);



            }

            else if (waist == null){

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Are you sure?");
                builder.setMessage("If you let Waist circumference empty, we will ask it from you later.")
                        .setCancelable(true)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                Intent i = new Intent(getApplicationContext(), number3.class);
                                i.putExtra("height" , hei);
                                i.putExtra("weight" , wei);
                                startActivity(i);
                                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

                            }


                        });
                AlertDialog alert = builder.create();
                alert.show();




            }

            else{

                Log.d("BMI Height" , String.valueOf(wei));
                Log.d("BMI Weight" , String.valueOf(hei));
                Intent i = new Intent(getApplicationContext(), healthcalculator3.class);
                i.putExtra("height" , hei);
                i.putExtra("WC" , waist);
                i.putExtra("weight" , wei);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);


            }

即使所有这些(腰,纬,黑)都为空或没有设置值,它只会激活最后一个ELSE,并且机器认为它们有一些数据/值。

3 个答案:

答案 0 :(得分:1)

project.lock.json

答案 1 :(得分:1)

你也可以试试这个

theTabsCompleteDataObject

答案 2 :(得分:0)

比较字符串的最佳方法是使用stringInstance.equals("what to compare"),因此您的代码将为:

hei.equals("")

你也可以使用字符串的长度:

hei.length() > 0