Cannot use any variable as argument to .setHint()

514 Views Asked by At
// This is in the class which calls the next activity with an intent:
Bundle bundle = new Bundle();
bundle.putCharSequence("Hint", "test");
startActivityForResult(new Intent(this, PosAct.class)
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
.putExtras(bundle)
, request_code);

// This is in the PosAct activity which is being called by the class above:
// 1 - Fails!
CharSequence temp = bundle.getCharSequence("Hint");
((EditText) findViewById(R.id.editTextFloor)).setHint(temp);

// 2 - Fails!
((EditText) findViewById(R.id.editTextRoom))
.setHint((CharSequence)bundle.getCharSequence("Hint"));

Toast.makeText(getApplicationContext(), bundle.getCharSequence("Hint"), Toast.LENGTH_LONG).show();

// 3 - Works perfectly!
((EditText) findViewById(R.id.editTextStreet)).setHint("test");

I try to set hint texts using java code instead of xml, but I fail to use any variable as argument to the setHint() method. Above I make tries in 3 different EditText.

Nr 1 above doesn't work. The EditText remains empty. Nr 2 above has the same result, but the Toast displays correctly ("test"). Nr 3 above works perfectly, the EditText has "test" as its hint text.

I tried first using String with same results. Explicitly using CharSequence didn't help. What is this all about???

1

There are 1 best solutions below

1
On

Try this..

 Intent intent = new Intent(this, PosAct.class)
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);


        //load the intent with a key "hint" and assign it's value
        //to be whatever has been entered into the text field...
        intent.putExtra("hint","test");

In your activity you are passing to use this..

    Bundle extras = intent.getExtras();
    mEditText1.setHint(extras != null ? extras.getString("hint"):"nothing