TJ1
TJ1

Reputation: 8520

Could not find method in a parent or ancestor Context for android:onClick attribute

I have an image that accepts click and should open an xml layout when clicked.

Here is the xml file that contains the clickable image:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.abcd.myapp.myabcdapp.activities.BeginnersActivity">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:onClick="openHelp1"
            .../>

As it can be seen in the tools:context I have included BeginnersActivity. Here is what I have in BeginnersActivity.java:

public class BeginnersActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beginners);
        UiHelper.hideSystemBar(this);
    }
    public void openHelp1(View view) {
        Intent i = new Intent(this, Help1Activity.class);
        startActivity(i);
    }
    ...

And here is what I have in Help1Activity.java:

public class Help1Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help1);
    }
    ...

I have also added Help1Activity to the AndroidManifest.xml as:

....
<application
        android:name=".MyApp"
        android:allowBackup="true"
        android:configChanges="locale|touchscreen|orientation|screenLayout|screenSize|keyboardHidden|uiMode"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
        <activity android:name=".activities.Help1Activity" />
        ....

When I click on imageView1 the app crashes and in debug mode I see this message:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method openHelp1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'imageView1'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)

Is there anything I am missing or doing incorrectly?

Upvotes: 0

Views: 3235

Answers (2)

Stanislav Tkach
Stanislav Tkach

Reputation: 69

You must have "openHelp1" method in same activity class where you use xlm file with attribute android:onClick="openHelp1"

You can't call method from another activity in this way.

Upvotes: 1

jspcal
jspcal

Reputation: 51934

You can avoid this by setting an OnClickListener directly:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help1);

    ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);

    imageView1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            openHelp1(view);
        }
    });
}

Upvotes: 0

Related Questions