3 回答

TA貢獻1864條經驗 獲得超2個贊
您錯誤地使用了屬性。這是錯誤的:onClick
android:onClick="startActivity()"
它應該是:
android:onClick="startActivity"
在 https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents 閱讀更多內容
建議
應避免在 xml 中使用。請改用 。將邏輯布局和 UI 布局分開非常重要,這樣在更改 xml 布局時就不需要考慮太多。使用類似如下的內容:android:onClickonClickListener
Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something here when button is clicked.
}
});

TA貢獻1801條經驗 獲得超8個贊
正如??? ???? ????9A https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents
Responding to Click Events
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
//onClick function/method name don't use round brackets
android:onClick="sendMessage" />
和在您的活動中
//JAVA
/** Called when the user touches the button */
public void sendMessage(View view) {
// Do something in response to button click
}
//Kotlin
/** Called when the user touches the button */
fun sendMessage(view: View) {
// Do something in response to button click
}
并刪除tools:ignore="OnClick"
我希望它有幫助

TA貢獻1815條經驗 獲得超6個贊
首先,改變這一點
android:onClick="startActivity()"
對此:
android:onClick="startActivity"
然后將 startActivity 方法移到 OnCreate 方法下方。它目前在OnCreate內部
添加回答
舉報