3

I add this in my MainActivity.java

btn = (Button)findViewById(R.id.aboutTheDeveloper);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void openAboutTheDeveloper(View v) {
        startActivity(new Intent(MainActivity.this, aboutTheDeveloper.class));
    }
}

I have also edited my manifests:

    <activity
        android:name=".aboutTheDeveloper"
        android:label="@string/title_activity_about_the_developer" >
    </activity>

Scenario:

I have this button inside MainActivity.xml with id = @id/openAboutTheDeveloper

Which should open the AboutTheDeveloper Activity.

Inside button I also placed android:onclick="openAboutTheDeveloper"

Which should call function openAboutTheDeveloper inside MainActivity.java

It doesn't seem to work. T_T

Sorry if I'm too explicit. I'm very new to Android and I'm not really friends with Java.

martijnn2008
  • 3,552
  • 5
  • 30
  • 40
Kinna P
  • 39
  • 2
  • 4

1 Answers1

5

you can't have

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void openAboutTheDeveloper(View v) {
        startActivity(new Intent(MainActivity.this, aboutTheDeveloper.class));
    }
}

The View.OnClickListener interface has no public void openAboutTheDeveloper(View v) method, but public void onClick(View v). You can either change your onClickListener like

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, aboutTheDeveloper.class));
    }
}

removing the android:onclick property from your xml, or you get rid of that code, and add a method inside MainActivity

public void openAboutTheDeveloper(View v) {
        startActivity(new Intent(MainActivity.this, aboutTheDeveloper.class));
}

the property android:onclick let you declare a handler that will be invoked at runtime. The method will be resolved with the reflection and if it is not found, Android throws an exception

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Wow! Thanks @Blackbelt. I retained the onClick = "openAboutTheDeveloper" and added the method openAboutTheDeveloper() – Kinna P Aug 28 '15 at 18:56