2014-01-28 2 views
0

나는이 같은 활동의 텍스트 뷰에 메시지를 표시하려고 : "안녕하세요!"안드로이드 텍스트 뷰의 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!이 표시되지 않았습니다.

<?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> 

메시지를 보내 주요 활동 : "안녕하세요!"

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를 보내는 책임이 주요 활동에 대한 코드를 추가 한 의미하지 않는다 너무 – njzk2

답변

2

당신이 표시되어 있습니다를 enter image description here

enter image description herestrings.xml입니다 인 텐트에 포함 된 여분의 메시지가 null 인 경우. 그러나 당신의 코드에서 당신은 오직 의도 된 엑스트라들만을 확인하고 있습니다. 의도에서 다른 매개 변수를 전달할 수 있으므로 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()은 널 (null)을 리턴하지 않습니다. 따라서 intent.getStringExtra(MainActivity.EXTRA_MESSAGE)""을 반환하는지 확인한 다음 그에 따라 작업하십시오. 행운을 빕니다!()! = null이가 intent.getStringExtra가 null가 아닌

관련 문제