Reputation: 1
In Android Studio, I'm getting an error that the onClick listener(which was generated on the XML page by Android Studio) cannot be found. I haven't had any problems previously until today, so I can't figure out what is wrong. I have already tried removing the android:theme tags, but that did not help.
Here is the following error message that I'm getting:
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class android.support.v7.internal.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatEditText with id 'subsetTextField'
at android.view.View$1.onClick(View.java:4273)
at android.view.View.performClick(View.java:5242)
at android.widget.TextView.performClick(TextView.java:10530)
at android.view.View$PerformClick.run(View.java:21185)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
at java.lang.Class.getMethod(Class.java:671)
at android.view.View$1.onClick(View.java:4266)
at android.view.View.performClick(View.java:5242)
at android.widget.TextView.performClick(TextView.java:10530)
at android.view.View$PerformClick.run(View.java:21185)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
The XML:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/subsetTextField"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClick" />
The Java in the Main Activity(which is currently empty, but can't be found):
public void onClick(View view) {
}
The full MainActivity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView output;
EditText setInput;
EditText subsetInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
output = (TextView) findViewById(R.id.outputTextView);
setInput = (EditText) findViewById(R.id.setTextField);
subsetInput = (EditText) findViewById(R.id.subsetTextField);
output.setFocusable(false);
}
@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_main, 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 int factorial(int n) {
int fact = 1; // this will be the result
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public void onClick(View view) {
}
I just added a button and tied the listener to the button and everything seems to be working just fine now, so I'm not exactly certain what caused the issue, but it's resolved now. Thanks for the assistance.
Upvotes: 0
Views: 1532
Reputation: 79
I had the same issue. I found out that my application context was set to the wrong activity because I had copy/paste the activity & .xml file. Here an example:
Upvotes: 0
Reputation: 885
Instead of adding onClick() from XML add it programmatically:
EditText myEditText = (EditText) findViewById(R.id.subsetTextField);
myEditText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Upvotes: 1