When I click my button in emulator, my application crashes.
Below is my layout.xml:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:onClick="loginUser"
android:text="@string/login_text" />
Here is my method loginUser() in my MainActivity.java:
public void loginUser() {
Log.d("User Authentication Service", "Trying to login...");
doLogin(new Intent(getBaseContext(), LoginUserService.class));
}
I've defined which method should be called in my layout.xml using the below line:
android:onClick="loginUser"
But when I press the button in emulator, application crashes. I've tried changing the above value in layout.xml as show below:
android:onClick="loginUser()"
But nothing works. What's is wrong?
You are missing the parameter View
in your method signature.
Try changing your code as shown below:
public void loginUser(View view) {
...
}
EDIT:
And it should be android:onClick="loginUser"
in your layout.xml
.
Reference: http://developer.android.com/reference/android/widget/Button.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.