daiyue
daiyue

Reputation: 7458

android TextView setText issue

I tried to display a message on a TextView in a activity like this:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Get the message from the intent
        Intent intent = getIntent();

        // Create the text view
        TextView textView = (TextView) findViewById(R.id.display_message);      

        if(intent.getExtras() != null){
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

     // Show the Up button in the action bar.
    setupActionBar();
}

The code supposed to display "hello world!" when the extra message contained in the intent is null, but instead hello world! did not show up.

enter image description here enter image description here

This is my strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Action Bar</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

The main activity which send the message:

public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);

    return super.onCreateOptionsMenu(menu);
    //return true;
}

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_search:
    //  openSearch();
        return true;
    case R.id.action_settings:
    //  openSettings();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }   
}   
}

Upvotes: 0

Views: 1390

Answers (3)

ravindra.kamble
ravindra.kamble

Reputation: 1023

You can check particular value, whether it is null or it has value. And then add your display logic as follows

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Get the message from the intent
        Intent intent = getIntent();
        String message = null;

        // Create the text view
        TextView textView = (TextView) findViewById(R.id.display_message);      

        if(intent != null){
            message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        }
        if(message != null && message.length() > 0){
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

     // Show the Up button in the action bar.
    setupActionBar();
}

Upvotes: 2

Bipin Bhandari
Bipin Bhandari

Reputation: 2692

intent.getExtras() does not returns null. So what you can do is check if intent.getStringExtra(MainActivity.EXTRA_MESSAGE) returns "" and work accordingly. Good luck!

Upvotes: 1

Antrromet
Antrromet

Reputation: 15424

You are supposed to display "hello world!" when the extra message contained in the intent is null. But in your code you are checking only for the intent extras. You might be passing some other params in the intent and therefore intent.getExtras() will not return null.

You should try something along the lines

if(intent.getExtras() != null && !Textutils(intent.getStringExtra(MainActivity.EXTRA_MESSAGE))){
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

Upvotes: 2

Related Questions