gpt4 book ai didi

java - 无法从始终为空的包中获取值

转载 作者:行者123 更新时间:2023-12-01 20:09:44 25 4
gpt4 key购买 nike

在尝试了多种不同的方式来表达以下程序后,我无法将值从一个 Activity 传递到另一个 Activity 。由于某种原因, bundle 似乎一直为空。我不认为这可能是权限问题。

主要 Activity :

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")
{
...
}

...
}

有人可以帮我发现这个错误吗?

最佳答案

不要

 if(type == 

if(type.equals("YOUR_STRING"))

您应该使用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();
}

关于java - 无法从始终为空的包中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46876018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com