Reputation: 6057
I have a Android button that I have set the onClick
attribute to the login()
method in it's respective Activity.
Here is the layout:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="io.github.james_parsons.fblafashionapp.LoginActivity">
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email"
android:hint="@string/email_hint"
android:singleLine="true"
android:layout_below="@+id/loginTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:textColor="@color/text_secondary" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/login_button_text"
android:id="@+id/loginButton"
android:background="@color/primary"
android:layout_marginTop="55dp"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:elevation="2dp"
android:onClick="login"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_title"
android:id="@+id/loginTitle"
android:textColor="@color/text_primary"
android:textSize="28sp"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="@string/password_hint"
android:textColor="@color/text_secondary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/error"
android:layout_below="@+id/loginButton"
android:layout_centerHorizontal="true"
android:textColor="@color/error"
android:layout_marginTop="30dp" />
</RelativeLayout>
Here is my Activity:
package io.github.james_parsons.fblafashionapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
public class LoginActivity extends Activity {
public EditText email;
public EditText password;
public TextView error;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void login() {
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
error = (TextView) findViewById(R.id.error);
ParseUser.logInInBackground(email.getText().toString(), password.getText().toString(), new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Success
} else {
error.setText("Error logging in. Please try again later.");
}
}
});
}
}
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.james_parsons.fblafashionapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".Global"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" >
</activity>
</application>
</manifest>
The exception looks like this:
09-25 11:42:04.533 2094-2094/io.github.james_parsons.fblafashionapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.github.james_parsons.fblafashionapp, PID: 2094
java.lang.IllegalStateException: Could not find method login(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'loginButton'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Why can't Android find my method? The only method the layout can find is setListFooter
from android.preference.PreferenceActivity
.
Upvotes: 1
Views: 2882
Reputation: 3
In addition, the View parameter needs to be passed because the method is to be accessed by a view(Button). It would very well work without the parameter if the method is being called within another method.
Upvotes: 0
Reputation: 1077
Your login() method signature should contain View parameter.
public void login(View view) {
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
error = (TextView) findViewById(R.id.error);
ParseUser.logInInBackground(email.getText().toString(), password.getText().toString(), new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Success
} else {
error.setText("Error logging in. Please try again later.");
}
}
});
}
Upvotes: 4
Reputation: 4463
Your public void login()
method has the wrong signature. It needs to take a View
as a parameter.
Upvotes: 3