2012-05-26 46 views
0

我試圖根據this link,運行「Clean Code」,但是沒有發生任何事情。作爲一名叛徒開發者,我的直覺表示「Button onClick」缺失,但我可能是錯的。這裏是我的代碼,根據指令是什麼寫着:根開發者頁面的Android步驟1頁面活動不起作用

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 

public class BuildingaSimpleUserInterfaceApi8Activity extends Activity { 
    public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    /**This is how I work with Buttons and the Next Activity, however this method IS NOT defind 
    * in the very first Android Tutorial for sending information onClick. 
      Button bButtonone = (Button) findViewById(R.id.button_send); 
      bButtonone.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY")); 
        } 
       }); 
     } 
     */ 

    /** Called when the user selects 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); 
    } 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.developer.android.com_training_basics_firstapp_building_ui" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".BuildingaSimpleUserInterfaceApi8Activity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name="com.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" /> 

    </application> 

</manifest> 
+0

確保您更新了佈局以包含'android:onClick =「sendMessage」',如您在引用的頁面上的「響應發送按鈕」部分所述。 – CommonsWare

+0

這是我的佈局...我已經從指令中扣除了任何東西(但添加了一個菜單 - 退出函數,如代碼所示) KaSiris

+0

而作爲一個注過的過程中過去2到3年,我創建併發布了所有這些應用程序http://appgravity.com/android-apps;jsessionid=C7FF5AF35C74576D32DA84FA4E1A8772?pub=KaSiris – KaSiris

回答

2

看起來你忘了行啓動活動:

public void sendMessage(View view) { 
    ... 
    intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); // add me! 
} 
+0

謝謝薩姆......你的回答實際上是正確的,並且過程起作用。由於你的緣故,我正式去建立「清潔代碼」。 – KaSiris

1

我通讀在網站列出的代碼和您錯過了一行:

startActivity(intent); 

末尾od sendMessage。

+0

你的回答也是正確的,但Sam發送按鈕的速度更快一些。 – KaSiris

+0

它總是發生。 :) – cstrutton