0

After trying many different way to express the following program, I wasn't able to get pass a value from an activity to another. It seems that the bundle keeps being null for some reason. I don't think it could be a permission problem.

MainActivity :

public class MainActivity extends AppCompatActivity {

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

    public void ButtonRAWclick(View view)
    {
        Intent intent = new Intent(MainActivity.this
                , RestClient.class);
        intent.putExtra("type", "RAW_TYPE");
        startActivity(intent);
    }

    public void ButtonHTTPclick(View view)
    {
        Intent intent = new Intent(MainActivity.this
                , RestClient.class);
        intent.putExtra("type", "HTTP_TYPE");
        startActivity(intent);
    }

    public void ButtonJSONclick(View view)
    {
        Intent intent = new Intent(MainActivity.this
                , RestClient.class);
        intent.putExtra("type", "JSON_TYPE");
        startActivity(intent);
    }

}

RestClient :

public class RestClient extends AppCompatActivity implements SensorListener {
    RawHttpSensor rhs1;
    TextSensor rhs2;
    TextView temperature;
    String output;
    String type;

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

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getString("type");
        }

        if (type == "RAW_TYPE") {
            rhs1 = new RawHttpSensor();
            temperature = (TextView) findViewById(R.id.temperature);
            rhs1.registerListener(this);
            rhs1.getTemperature();
        }

        if (type == "HTTP_TYPE")
        {
            rhs2 = new TextSensor();
            temperature = (TextView) findViewById(R.id.temperature);
            rhs2.registerListener(this);
            rhs2.getTemperature();
        }

        if (type == "JSON_TYPE")
        {
           ...
        }

      ...
    }

Can someone help me spot the bug ?

Philippe
  • 700
  • 1
  • 7
  • 17

1 Answers1

3

Don't

 if(type == 

Do

if(type.equals("YOUR_STRING"))

You should use equals()

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

if (type.equals("RAW_TYPE")) {
        rhs1 = new RawHttpSensor();
        temperature = (TextView) findViewById(R.id.temperature);
        rhs1.registerListener(this);
        rhs1.getTemperature();
    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198