0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TJ1
  • 7,578
  • 19
  • 76
  • 119

2 Answers2

1

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.

0

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);
        }
    });
}
jspcal
  • 50,847
  • 7
  • 72
  • 76
  • Thanks for the response. Where shall I put this code? – TJ1 Jun 23 '18 at 14:46
  • In `onCreate` or after the layout is inflated. – jspcal Jun 23 '18 at 14:48
  • I get some errors: @Override it says `method does not overriode method from its superclass`. Also Cannot resolve symbol `OnClickListener` same error for `View` and for `openHelp1`. – TJ1 Jun 23 '18 at 14:57
  • Sorry I am a beginner, how can I import the `openHelp1` class? – TJ1 Jun 23 '18 at 15:14
  • I get other errors now. Is there a way to import `openHelp1` from the class I defined it in the `Help1Activity.java`? – TJ1 Jun 23 '18 at 15:55