Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Intent Extra不等于if语句中的值_Java_Android - Fatal编程技术网

Java Intent Extra不等于if语句中的值

Java Intent Extra不等于if语句中的值,java,android,Java,Android,这是我在这里发布的第一个问题,所以我希望我已经彻底和清楚地解释了我所面临的问题。任何/所有的帮助都将不胜感激 以下是我正在处理的java文件: MainActivity.java public class MainActivity extends AppCompatActivity { ImageButton name1Button; ImageButton name2Button; ImageButton name3Button; @Override

这是我在这里发布的第一个问题,所以我希望我已经彻底和清楚地解释了我所面临的问题。任何/所有的帮助都将不胜感激

以下是我正在处理的java文件:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ImageButton name1Button;
    ImageButton name2Button;
    ImageButton name3Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Layout contains 3 ImageButtons "@+id/imageButton1", "@+id/imageButton2" and "@+id/imageButton3"

        name1Button = (ImageButton) findViewById(R.id.imageButton1);
        name2Button = (ImageButton) findViewById(R.id.imageButton2);
        name3Button = (ImageButton) findViewById(R.id.imageButton3);
    }

    public void onChangeScreen(View view) {
        Intent changeScreenIntent = new Intent(this, SecondActivity.class);

        if(view == name1Button) {
            changeScreenIntent.putExtra("Name", "name1");
        } else if (view == name2Button) {
            changeScreenIntent.putExtra("Name", "name2");
        } else if (view == name3Button) {
            changeScreenIntent.putExtra("Name", "name3");
        } else {
            changeScreenIntent.putExtra("Name", "Other");
            }
        startActivity(changeScreenIntent);
    }
}
public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout); //Contains a TextView "@+id/textViewName"

        Intent myIntent = getIntent();
        String strName = myIntent.getExtras().getString("Name"); //Set strName to the parsed name ("name1", "name2" or "name3")
        TextView myTextView = (TextView) findViewById(R.id.textViewName);
        myTextView.setText(strName); //Sets the name parsed to a TextView //Setting the texts of the displayed TextView "@+id/textViewName" to the value of strName. This CORRECTLY shows the parsed name.

        if(strName == "name1") { // Never true, even though the value of strName is "name1"
            //Do thing if certain button clicked
            Toast.makeText(this, "You selected name1", Toast.LENGTH_SHORT).show();
        } else {
            //Do other thing if non-specified button clicked
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
    }
}
SecondActivity.java

public class MainActivity extends AppCompatActivity {

    ImageButton name1Button;
    ImageButton name2Button;
    ImageButton name3Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Layout contains 3 ImageButtons "@+id/imageButton1", "@+id/imageButton2" and "@+id/imageButton3"

        name1Button = (ImageButton) findViewById(R.id.imageButton1);
        name2Button = (ImageButton) findViewById(R.id.imageButton2);
        name3Button = (ImageButton) findViewById(R.id.imageButton3);
    }

    public void onChangeScreen(View view) {
        Intent changeScreenIntent = new Intent(this, SecondActivity.class);

        if(view == name1Button) {
            changeScreenIntent.putExtra("Name", "name1");
        } else if (view == name2Button) {
            changeScreenIntent.putExtra("Name", "name2");
        } else if (view == name3Button) {
            changeScreenIntent.putExtra("Name", "name3");
        } else {
            changeScreenIntent.putExtra("Name", "Other");
            }
        startActivity(changeScreenIntent);
    }
}
public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout); //Contains a TextView "@+id/textViewName"

        Intent myIntent = getIntent();
        String strName = myIntent.getExtras().getString("Name"); //Set strName to the parsed name ("name1", "name2" or "name3")
        TextView myTextView = (TextView) findViewById(R.id.textViewName);
        myTextView.setText(strName); //Sets the name parsed to a TextView //Setting the texts of the displayed TextView "@+id/textViewName" to the value of strName. This CORRECTLY shows the parsed name.

        if(strName == "name1") { // Never true, even though the value of strName is "name1"
            //Do thing if certain button clicked
            Toast.makeText(this, "You selected name1", Toast.LENGTH_SHORT).show();
        } else {
            //Do other thing if non-specified button clicked
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
    }
}
其目的如下:

  • MainActivity显示3个按钮
  • 用户按下3个按钮中的1个,所有按钮都会调用
    onChangeScreen
  • 根据3个按钮中用于调用更改屏幕上的
    onChangeScreen
    ,会向SecondActivity传递不同的值(字符串)
  • 设置要传递给SecondActivity的字符串后(使用if语句和
    changeScreenIntent.putExtra()
    ),调用第二个Activity
  • SecondActivity显示一个文本框,该文本框设置为使用
    .putExtra()
    传递的值
  • 然后使用if语句根据传递的字符串执行某些操作,该字符串基本上是基于调用SecondActivity的按钮

这就是问题所在。比较传递到SecondActivity的字符串的if语句“显然”不等于字符串的值(但显示的
TextView
显示了此字符串)。因此代码(替换为Toast)在if语句中从不使用。

使用
equals
而不是
=
比较字符串

使用


使用
等于
而不是
=
比较字符串

使用


始终用于比较字符串

if(strName.equals("name1"))
 Toast.makeText(this, "You selected name1", Toast.LENGTH_SHORT).show();
        } else {
            //Do other thing if non-specified button clicked
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
字符串的等于()而不是==


喜欢编码….

始终用于比较字符串

if(strName.equals("name1"))
 Toast.makeText(this, "You selected name1", Toast.LENGTH_SHORT).show();
        } else {
            //Do other thing if non-specified button clicked
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
字符串的等于()而不是==


享受编码的乐趣….

使用
名称1.equals(strName)
而不是
=
。如果
strName
为空,则使用
名称1.equals(strName)
而不是
=
。如果
strName
曾经是
null

您是否通过xml调用了此方法
onChangeScreen
?使用equals比较字符,使用“==”比较字符。您是否通过xml调用了此方法
onChangeScreen
?使用equals比较字符,使用“==”比较字符..我也喜欢把字符串常量放在第一位以避免空检查,即使它看起来不太好。我也喜欢把字符串常量放在第一位以避免空检查,即使它看起来不太好。