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

标签 java android

在尝试了多种不同的方式来表达以下程序后,我无法将值从一个 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/

相关文章:

java - 将嵌入式 Oracle JVM 中运行的 Java 按名称授予 Oracle 目录的等效语法是什么?

java - 安卓 SQLite : Data being added again and again

Android:使用 VPNService API 在 Android 4.0 中连接到 VPN 的示例代码

java - 方法没有重写或实现父类(super class)型错误的方法

java - 在 getter/setter 中创建一个计数器?

java - 如何先按值对 HashMap 进行排序,然后在重复的情况下按键对 HashMap 进行排序?

android - 如何从 Firebase 接收具有特定字符串 android 的数据

java - 退出时的后退按钮会产生强制关闭错误

android - 以编程方式在 TextView 中设置左可绘制对象

Java 客户端无法使用 WCF 服务