2011-07-23 51 views
0

我對Android和Java非常陌生,但這個網站一直幫助我,直到現在,謝謝大家!如何將按鈕分配給Android中的活動?

我正在構建一個應用程序,它有兩個選項卡,在一個選項卡中我創建了一些按鈕,並在相應的活動中有一個OnClickListener。當我運行它強制關閉應用程序,我得到的錯誤:

ERROR/AndroidRuntime(25971): java.lang.IllegalStateException: Could not find a method myClickHandler(View) in the activity class com.test.rate.MainActivity for onClick handler on view class android.widget.Button with id 'CalculateButton' 

這也可能是很簡單的,但幫助!

好吧,是我應該補充一些代碼:

的活動:

public class MetricActivity extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.metriclayout); 


    final Button button = (Button) findViewById(R.id.CalculateButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
     } 
    }); 

而且在標籤佈局按鈕:

<Button android:layout_height="wrap_content" android:text="Calculate" android:layout_width="wrap_content" android:id="@+id/CalculateButton" android:onClick="myClickHandler" android:layout_gravity="center"></Button> 
+0

請發佈您的代碼。 – Emiam

+1

在您實施相應方法的地方發佈您的活動的一些代碼。 –

回答

0

我猜你可能在你的xml文件中設置android:clickable =「true」,那麼你沒有在你的活動中定義一個onClickListener。在您的onCreate()的活動的法定義onClickListener您的按鈕:

Button btn = (Button) findViewById(R.id.yourbuttonid); 
btn.setOnClickListener(new OnClickListener(){ 

    public void onClick(View v) { 
     //do whatever when your button is clicked 
    } 

}); 
0

你不需要說:

android:onClick="myClickHandler" 

清除它,其餘的將被罰款。

相關問題