在尝试了许多不同的方式表达以下程序后,我无法将活动中的值传递给另一个活动。由于某种原因,捆绑似乎保持为空。我不认为这可能是一个许可问题。
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")
{
...
}
...
}
有人可以帮我发现错误吗?
答案 0 :(得分:3)
<强>不强>
if(type ==
<强>不要强>
if(type.equals("YOUR_STRING"))
您应该使用equals()
将此字符串与指定对象进行比较。如果,结果是真的 并且仅当参数不为null并且是String对象时 表示与此对象相同的字符序列。
if (type.equals("RAW_TYPE")) {
rhs1 = new RawHttpSensor();
temperature = (TextView) findViewById(R.id.temperature);
rhs1.registerListener(this);
rhs1.getTemperature();
}