2014-01-28 45 views
0

我想在这样的活动上显示一个TextView的消息:“Hello World”的机器人的TextView的setText问题

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(); 
} 

应该显示的代码当包含在intent中的额外消息是null,而不是hello world!没有显示出来。

enter image description here enter image description here

这是我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> 

主要活动,其发送的消息: “Hello World” 的

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); 
    } 
} 
} 
+0

我已经添加了负责发送消息的主要活动代码 – daiyue

+0

intent.getExtras()!= null并不意味着intent.getStringExtra也不为null – njzk2

回答

2

你应该显示当意图中包含的额外消息为空时。但在你的代码中,你只是检查意图的演员。您可能会传递一些其他参数,因此intent.getExtras()将不会返回null。

你应该尝试的东西沿线

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); 
     } 
+0

我想你的意思是使用'!TextUtils.isEmpty (intent.getStringExtra(MainActivity.EXTRA_MESSAGE))' – daiyue

2

您可以检查特定的价值,无论是空或它的价值。然后添加您的显示逻辑如下

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(); 
} 
1

intent.getExtras()不返回空值。所以你可以做的是检查是否intent.getStringExtra(MainActivity.EXTRA_MESSAGE)返回""并相应地工作。祝你好运!