2013-06-28 32 views
0

我正在使用創建MyFirstApp的教程。我已經到了一個地步,我已經遵循了所有的教程,並且發現它不起作用。教程中提到「現在您可以運行應用程序,打開它時,在文本字段中輸入消息,單擊發送,並且消息出現在第二個活動中。」但它不會在我的設備上工作,發送按鈕除了在按下時始終變爲藍色以外不做其他任何操作。我給這點代碼,爲的onCreate(捆綁)是:當在我的手機上運行時,發送按鈕仍然無能爲力?

@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

    // Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    // Set the text view as the activity layout 
    setContentView(R.layout.activity_display_message); 

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     // Show the Up button in the action bar. 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

而且,我activity_main.xml中:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage" /> 

和我MainActivity.java部分:

/** Called when the user clicks the send button */ 
public void sendMessage (View view) { 
    // Do something in response to button 
    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); 
} 
+0

我們可以看到onClick的代碼嗎? – Jbad26

+0

@ Jbad26當然,我已經編輯了包含來自其他文件的部分的問題。我希望我已經添加了相關部分。 – Snodge182

+0

這對我的解決方案工作非常有幫助,或者您仍然遇到問題? – Jbad26

回答

1

對於我在週末離開電腦的響應時間感到抱歉。這就是說你在sendMessage函數的結尾缺少了這一行:

startActivity(intent); 

這告訴活動以您創建的意圖開始。現在,您正在創建一個意圖,但不會將它發送到任何地方。希望有所幫助。

相關問題