我對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>
請發佈您的代碼。 – Emiam
在您實施相應方法的地方發佈您的活動的一些代碼。 –