2017-10-22 8 views
0

次のプログラムを表現するためにさまざまな方法を試した結果、アクティビティから別のプログラムに値を渡すことができませんでした。何らかの理由でバンドルがnullのままになっているようです。私は許可の問題ではないと思う。常にヌルバンドルから値を取得できません

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

     ... 
    } 

誰かが私にはバグを見つける手助けすることはできますか?

答えて

3

ない

if(type == 

あなたはequals()

は、指定されたオブジェクトにこの文字列を比較使うべき

if(type.equals("YOUR_STRING")) 

を行います。 で、引数がnullでなく、 がこのオブジェクトと同じ文字シーケンスを表すStringオブジェクトである場合のみ、結果は真です。

if (type.equals("RAW_TYPE")) { 
     rhs1 = new RawHttpSensor(); 
     temperature = (TextView) findViewById(R.id.temperature); 
     rhs1.registerListener(this); 
     rhs1.getTemperature(); 
    } 
関連する問題