2013-10-12 105 views
2

我今天安裝了Android的SDK包和我下面的「我的第一個應用程序」教程中,我被卡住,它指出:響應發送按鈕

打開MainActivity類別(位於項目的src /目錄下)並添加相應的方法:

/** Called when the user clicks the Send button */ 
public void sendMessage(View view) { 
    // Do something in response to button 
} 

我在哪裏把這個文件?這是「MainActivity.java」文件嗎?

我試過了,我不斷收到錯誤,所以我顯然在某處出錯了。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" 
     android:onClick="sendMessage" /> 
</LinearLayout> 

MainActivity.java

package com.example.myfirstapp; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.Menu; 



public class MainActivity extends Activity { 

    @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, menu); 
     return true; 
    } 

} 

希望我已經作出了明確的問題,我看了論壇上的一個答案,但我不能」找不到任何東西。

+2

錯誤是什麼? ..你可以發佈完整的活動代碼? – Shubhank

回答

3

如果你在你(說activity_main.xml)XML佈局的按鈕,你有按鈕

android:onClick="sendMessage" 

下面的屬性,你有以下MainActiivty.java

setContentView(R.layout.activity_main); 

你應該有在MainActivity.java

/** Called when the user clicks the Send button */ 
public void sendMessage(View view) { 
    // Do something in response to button 
} 

例如:

MainActivity.java

// Your imports 
public class MainActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); //setting the layout to activity 
} 
/** Called when the user clicks the Send button */ 
public void sendMessage(View view) { 
    // Do something in response to button 
} 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 

    // other widgets 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="146dp" 
     android:onClick="sendMessage" 
     android:text="Button" /> 

</RelativeLayout> 
+0

本教程沒有提及「setContentView(R.layout。activity_main);「 –

+0

@DavidBateman沒有足夠的信息請發佈你在教程中有什麼你正在爲你的活動設置佈局 – Raghunandan

+0

我只是困惑,我把」公共無效sendMessage(視圖視圖){「,我是新的開發。本教程對我來說看起來並不是很清楚,只是說將相應的方法添加到MainActivity類 –

0

如果你是一個新的Android開發者和做你的第一所以從基礎開始喜歡推出新的活動它包含的hello world或任何文本視圖,按鈕,然後你將清除有關應用程序的想法。

  1. 創建XML佈局你的Android應用程序
  2. 將按鈕和文本視圖
  3. 運行你的第一個應用程序。
  4. 你會得到你的輸出。
+0

我想要遵循http://developer.android.com上的教程/training/basics/firstapp/starting-activity.html,當按下「發送」按鈕時,一切都可以正常工作。當按下「發送」按鈕時,添加代碼以執行某些操作是令我困惑的。你能推薦一個更容易理解的教程嗎? –

0

放入MainActivity.java右上方後

public class MainActivity extends ActionBarActivity { 

完成這一步之後,您可能需要進口。通過按控制/移位/ O(非零)執行此操作